HOW TO: Create simple workflow activity in MS CRM 2015

Let's go throw Custom Workflow Activity creation process today.

To do that I need following:

  • Visual Studio 2015 and .NET Framework 4.5.2 installed on my dev. machine
  • Microsoft Dynamics CRM SDK 2015

1. Open Visual Studio and create new “Class Library” project

2. Add Reference
Press Add New Reference:


Click Browse on the left side and then button "Browse…":


Navigate to folder where you extracted Microsoft CRM SDK, select following assemblies: bin\Microsoft.Crm.Sdk.Proxy.dll;
bin\Microsoft.Xrm.Sdk.dll;
bin\Microsoft.Xrm.Sdk.Workflow.dll.

Press button "Add".


Press "Ok":


Add System.Activities assembly:

3. Rename Class1.cs to SimpleWorkflow.cs and change as shown below

using System;
using System.Activities;

namespace ExitoConsulting.WorkflowActivities
{
    public class SimpleActivity : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            var tracingService = activityContext.GetExtension();
            var workflowContext = activityContext.GetExtension();

            var serviceFactory = activityContext.GetExtension();
            var userService = serviceFactory.CreateOrganizationService(workflowContext.UserId);
            //var adminService = serviceFactory.CreateOrganizationService(null);
        }
    }
}

4. Implement business logic
Below you can find code which implement business logic (creates a task). Add input parameters and modify execute method as shown:

public class SimpleActivity : CodeActivity
{
    protected override void Execute(CodeActivityContext activityContext)
    {
        var tracingService = activityContext.GetExtension();
        var workflowContext = activityContext.GetExtension();

        var serviceFactory = activityContext.GetExtension();
        var userService = serviceFactory.CreateOrganizationService(workflowContext.UserId);

        tracingService.Trace("Creating task");

        var days = Days.Get(activityContext);
        var subject = Subject.Get(activityContext);

        Entity targetEntityImage = (Entity)workflowContext.InputParameters["Target"];

        Entity task = new Entity("task");
        task.Attributes.Add("subject", subject);
        task.Attributes.Add("scheduledstart", DateTime.Now.AddDays(days));
        task.Attributes.Add("scheduledend", DateTime.Now.AddDays(days));
        task.Attributes.Add("regardingobjectid", targetEntityImage.ToEntityReference());

        userService.Create(task);

        tracingService.Trace("Task was created");
    }

    [RequiredArgument]
    [Input("Subject")]
    public InArgument Subject { get; set; }

    [RequiredArgument]
    [Input("Days")]
    public InArgument Days { get; set; }
}

5. Enable assembly signing
Go to the project properties:


Go to the Signing tab and select <New…>:


Fill-in the form as shown:


Save changes:

6. Build the project 
Now we are ready to build the project:

7. Assembly Registration
We are going to use Plugin Registration tool. Plugin registration tool is provided as part of SDK and located under "SDK\Tools\PluginRegistration".

Open Plugin Registration tool. Press “+ Create New Connection” to create new connection. Fill-in appropriate fields (I have MS CRM on-prem.):

If you have several organization don't forget to check "Display list of available organizations" when creating new connection.

Press "Register new assembly":


Choose assembly you built on previous step:


Set friendly name and press "Save":


Now we have assembly and our custom activity registered.

8. Workflow creation
Let's create workflow and test our activity.

Go to "Settings" -> "Customizations":


Select Customize the System:


Press on "Process" and then on "New":


Fill-in the form as shown and press "Ok":


New form is opened. Fill-in as shown below and add created activity:


Press on "Set Property":


Set property as shown and save:


Save workflow and Activate it:

9. Testing
Create new account. Wait for a while (we created async workflow). Check for a task.

You can download Visual Studio project here: