Udostępnij za pośrednictwem


Create a custom workflow activity

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

This topic describes how to create a custom workflow activity and register it for use in Microsoft Dynamics 365 (online & on-premises). For Microsoft Dynamics 365 (online), custom workflow activities can only be registered to execute in the sandbox (partial trust). For more information about the sandbox and partial trust, see Plug-in isolation, trusts, and statistics.

In This Topic

Required software and assemblies

Use the CodeActivity workflow base class

Specify the name and group name for a custom workflow activity

Required software and assemblies

To develop Windows Workflow Foundation 4 custom activities for Dynamics 365, you must develop them on Microsoft .NET Framework 4.5.2. The following assemblies must be added as references in your project. They can be found in the SDK\Bin folder of the Microsoft Dynamics 365 SDK. Download the Microsoft Dynamics CRM SDK package.

  • Microsoft.Xrm.Sdk.dll

  • Microsoft.Xrm.Sdk.Workflow.dll

Use the CodeActivity workflow base class

To create a custom workflow activity, create a class that inherits from the CodeActivity workflow base class. This class is available in the System.Activities namespace. Activities that inherit from the CodeActivity class can override the Execute method to produce custom functionality.

  1. Start Microsoft Visual Studio.

  2. On the File menu, click New, and then click Project.

  3. In the New Project dialog box, select Workflow under Visual C# in the Installed Templates pane, and then select Activity Library.

  4. Specify a name and location for the solution, and then click OK.

  5. Navigate to the Project menu and select Properties. On the Application tab, specify .NET Framework 4.5.2 as the target framework.

  6. Add references to the Microsoft.Xrm.Sdk.dll and Microsoft.Xrm.Workflow.dll assemblies.

  7. Delete the Activity1.xaml file in the project.

  8. Add a class file (.cs) to the project. In Solution Explorer, right-click the project, select Add, and then click Class. In the Add New Item dialog box, type a name for the class, and then click Add.

  9. Open the class file, and add the following using directives:

    using System.Activities;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Workflow;
    
  10. Make the class inherit from the CodeActivity class and give it a public access modifier as shown here:

    public class SampleCustomActivity : CodeActivity
    
  11. Add functionality to the class by adding an Execute method:

    protected override void Execute(CodeActivityContext context){    //Activity code}
    

    For more information, see Add metadata to a custom workflow activity.

  12. Specify input and output parameters. For more information, see Add metadata to a custom workflow activity.

  13. In the project properties, under the Signing tab, select Sign the assembly and provide a key file name. Custom workflow activity (and plug-in) assemblies must be signed.

  14. Compile the project to create an assembly (.dll).

To view a code sample that demonstrates how to create a custom workflow activity, see Sample: Create a custom workflow activity.

Important

For improved performance, Microsoft Dynamics 365 caches custom workflow activity instances. The custom workflow activity’s Execute method should be written to be stateless because the constructor is not called for every invocation of the custom workflow activity. Also, multiple system threads could execute the custom workflow activity at the same time. All per invocation state information is stored in the context, so it is not recommended to use global variables or member variables to pass data from one invocation to the next.

Specify the name and group name for a custom workflow activity

When you register a custom workflow activity assembly, specify the name and group name. The name property specifies the name of the workflow activity. The group name property specifies the name of the submenu added to the main menu in the Microsoft Dynamics 365 process designer. These properties link the custom workflow activity with the Microsoft Dynamics 365 process designer, so that the custom activity name will appear in the user interface.

To specify the name and group name for a custom workflow activity, use the PluginType.Name and PluginType.WorkflowActivityGroupName attributes when you register the custom workflow activity assembly. For more information about registering custom workflow activities, see Register and use a custom workflow activity assembly. If the PluginType.Name and PluginType.WorkflowActivityGroupName attributes are set to null, the custom activity is hidden from the Microsoft Dynamics 365 workflow designer and is only accessible from XAML workflows.

If you are using the Plug-in Registration tool to register the custom workflow activity assembly, you can specify appropriate values in the Name and WorkflowActivityGroupName boxes, under the Editable region. For more information about using the Plug-in Registration tool, see Walkthrough: Register a plug-in using the plug-in registration tool.

Specify the Group Name and Name while registering

After this custom workflow activity is registered, you can use it from the Microsoft Dynamics 365 process designer for workflows or dialogs. For more information, see Register and use a custom workflow activity assembly.

See Also

Custom workflow activities (workflow assemblies)
Add metadata to a custom workflow activity
Use the IOrganization web service in a custom workflow activity
Sample: Create a custom workflow activity
Sample: Azure aware custom workflow activity
Windows Workflow Foundation 4 Base Activity Classes

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright