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:

15 comments

avatar

Great work .The information provided in the article is concise and clear.Thank you for sharing.VISIT-
CRM

avatar

Augurs is one of the best IT company that offers offshore custom CRM Software Development Services to clients across India, USA, UK, and HongKong.

avatar

It is really a great work and the way in which you are sharing the knowledge is excellent.
Microsoft Dynamics AX Online Training

avatar

The way you described the workflow activity shows your research on the project. Thank you for uploading such a blog on workflow activity. Keep up the excellent work.

avatar

Wow, Great information and this is very useful for us.

pomalex 4mg

avatar

Wow, Great information and this is very useful for us.

Crm software development company in chennai

avatar

Good piece of work and has got a clear explanation..Surya Informatics

avatar

Thanks for sharing such a great information with us.Keep on updating us with these types of blogs.
professional web design company in chennai

avatar

Your information was very clear. Thank you for sharing.
D365 Finance and Operations Online Training

avatar

Very Informative! CRM Software can be used to meet all your requirements and make your business run in pace. The blog was very good. Earned Quality knowledge. Thanks

avatar

Here is the best music to calm and relax your mind

1. best relaxing music
2. best Depp sleep music
3. best meditation music
4. best calm music
5. best deep focus music

avatar

Nice information about crm development. Thanks for sharing this information