Walkthrough: Creating and Registering a Custom Workflow Activity

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

This walkthrough shows you how to implement a custom workflow activity and register it using the plug-in registration tool. For the complete code listing of the workflow created in this walkthrough, see Custom Workflow Activity: Return a Calculated Value.

You will learn how to do the following:

  • Create a custom workflow activity.
  • Use the plug-in registration tool to register your custom workflow activity.
  • Call the custom workflow activity from the workflow designer.


In order to complete this walkthrough, you will need the following components:

For more information, see the following section in MSDN: http://go.microsoft.com/fwlink/?LinkId=169136 TARGET="_blank"Windows Workflow Foundation.

Creating the Custom Workflow Activity

In this section, you will create your custom workflow activity. You will create a project in Visual Studio, add dependency properties to contain the values used by your workflow activity, and finally you will override the Execute method to return the result to Microsoft Dynamics CRM.

Creating the Visual Studio Project

In this procedure, you will create the Visual Studio project that will contain your custom workflow activity, add references to the appropriate DLLs, and modify the using statements.

To Create the Visual Studio Project

  1. Open Visual Studio. From the File menu, choose New Project and then choose Workflow followed by Empty Workflow Project. Name the project "SampleWorkflows," and click OK.
  2. In Solution Explorer, right-click References and then click Add Reference. Select Browse, browse to the SDK\Bin directory, and then select Microsoft.Crm.Sdk.dll.
  3. In Solution Explorer, right-click References and then choose Add Reference. Select Browse, browse to the SDK\Bin directory, and then select Microsoft.Crm.SdkTypeProxy.dll.
  4. From the Project menu, select Add New Item and then choose Class. Name the class "QAAddActivity," and click OK.
  5. Replace the using statements section with the following code:
    using System;
    using System.Collections;
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.ComponentModel.Serialization;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    using System.Workflow.Activities.Rules;
    using System.Reflection;
    using Microsoft.Crm.Workflow;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
  6. Modify the class declaration so that your class inherits from Activity as shown here:
    class QAAddActivity : Activity
  7. Annotate the class declaration with the CrmWorkflowActivity attribute to mark this class as a custom workflow activity, as shown here. For more information about annotating your code, see Adding Metadata to the Custom Workflow Activity.
    [CrmWorkflowActivity("A sample add activity")]
    class QAAddActivity : Activity
  8. From the Build menu, choose Build SampleWorkflows to build and then save your work.

Adding Dependency Properties to the Workflow Activity

Dependency properties indicate that the workflow is dependent on this value. All values that are used as inputs or outputs for the custom workflow activity should be marked as dependency properties. For further information about dependency properties, see Dependency Properties or the dependency property topics in the .NET Framework documentation.

To Add Dependency Properties to Your Custom Workflow Activity

  1. Add the following code to the QAActivity class to create a property that holds a CRM input value. Note that the property is annotated with the CrmInput attribute.
    public CrmNumber a
            return (CrmNumber)base.GetValue(aProperty);
            base.SetValue(aProperty, value);
  2. Register the property that you created in the previous step as a dependency property by adding the following code to the QAActivity class:
    public static DependencyProperty aProperty =
  3. Repeat the previous two steps of this section to add another dependency property named 'b' as shown below:
    public static DependencyProperty bProperty =
    public CrmNumber b
            return (CrmNumber)base.GetValue(bProperty);
            base.SetValue(bProperty, value);
  4. Add a dependency property that represents the result of the workflow activity as shown here. Note that the property is annotated with the CrmOutput attribute, indicating that it is a value that will be returned to the calling workflow.
    public static DependencyProperty resultProperty =
    public CrmNumber result
            return (CrmNumber)base.GetValue(resultProperty);
            base.SetValue(resultProperty, value);
  5. From the Build menu, select Build SampleWorkflows to build and save your work.

Overriding the Execute method

The Execute method is used by custom workflow activities to run the activity and notify Microsoft Dynamics CRM that the activity has been completed. In this section, you will override the Execute activity to add code to calculate the Result property and complete execution. For more information, see Adding Metadata to the Custom Workflow Activity.

To override the Execute method

  1. Add the following code to the QAActivity class:
    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        result = new CrmNumber(a.Value + b.Value);
        return base.Execute(executionContext);
  2. From the Build menu, select Build SampleWorkflows to build and save your work.


Registering the Custom Workflow Activity

In this section you will register your custom workflow activity with your Microsoft Dynamics CRM server.

Note    You must be a member of the Deployment Managers group on your Microsoft Dynamics CRM server to complete this step. For more information, see Registering Plug-in.

Note    This step requires that the Plug-in Registration Tool has previously been built and added to Visual Studio as a plug in. If you have not yet done this, please complete Walkthrough: Build the Plug-in Registration Tool before continuing with this walkthrough.

To Register Your Custom Workflow Activity

  1. Open Visual Studio.
  2. From the Tools menu, select CRM Plugin Registration Tool. The Plugin Registration Tool opens with the Connections window open.
  3. In the Label box of the Connections window, type My Connection. In the Discovery Server box, type the http address of the discovery server. If a port is required, type the port in the Port box. If required, type the domain in the Domain box, and type your user name in the User Name box.
  4. If prompted to save changes, click Yes.
  5. If prompted for credentials, type your password in the Password box.
  6. In the Connections window, click Connect to connect to the Microsoft Dynamics CRM server.
  7. In the tab for your organization, select Register and then select Register New Assembly. The Register New Plugin window opens.
  8. Under Step 1, click the ellipses () button. The Select Plugin Assembly window opens.
  9. Browse to the location of your workflow activity DLL, select it, and click Open.
  10. In the Register New Plugin window, click Register Selected Plugins.

Note    If you receive an error at this point, it is possible that your web.config file is incomplete or has been corrupted. For a possible solution, see http://support.microsoft.com/kb/968793.

  1. Close the Plugin Registration Tool. Your workflow activity is now registered.

Testing your Custom Workflow Activity

Now that you have registered your custom workflow activity, all that remains is to test it. You will test your custom workflow activity by creating a new workflow in Microsoft Dynamics CRM, and adding your custom activity. For more information about creating workflows with the workflow designer, see Using workflows to automate common business tasks.

To Test Your Custom Workflow Activity

  1. Log on to Microsoft Dynamics CRM.
  2. In the Navigation pane, select Settings, and then select Workflows.
  3. In the Workflows pane, click New to open the Workflow: New dialog box.
  4. In the Workflow Name box, type Add 1000 to Credit Limit, and then click OK.
  5. In the Workflow dialog box, verify that the Scope is set to User and that the box next to Record is created is selected. This ensures that this workflow will be run only when accounts that are owned by the user are created.
  6. In the lower panel, click Add Step and then choose A sample add activity. This is the sample workflow activity that you created earlier in this walkthrough.
  7. In the text box type Add 1000 to credit limit.
  8. Click Set Properties to open the Set Custom Step Input Properties dialog box.
  9. In the value box for variable 'a', type 1000.
  10. Set the focus to the value box for variable 'b'. In the second drop-down box below Look For, choose Credit Limit and then click Add. Under Default Value, type 1000 and click OK. At the top of the dialog box, click Save and Close.
  11. In the lower panel of the Workflow dialog box, click Add Step and then select Update Record from the drop-down list.
  12. Click Set Properties to open the Update Account dialog box.
  13. Select the Administration tab. Set the focus to Credit Limit.
  14. In the first drop-down box under Look For, select Add 1000 to credit limit and verify that the second drop-down box displays result and click Add and then click OK.
  15. In the upper left-hand corner click Save and Close.
  16. In the Workflows pane, click Publish, and then click OK.
  17. In the Navigation pane, click Workplace, and then select Accounts.
  18. In the Accounts pane, click New.
  19. In the Account Name box, type Test. Click Save and Close.
  20. In the Accounts pane, double-click your new account. Under the Administration tab, verify that the credit limit is $2000. Note that this might not be reflected immediately, because workflows are run asynchronously.

See Also


Other Resources

© 2010 Microsoft Corporation. All rights reserved.