Friday, December 12, 2008

Your first Dynamics CRM WorkFlow Activity

This post show should act as a guideline to creating you first workflow activity dll for Dynamics CRM. It includes some gotachas I came across so hopefully it will save you some time.

Create a new workflow activity library in Visual Studio

You need to reference the CRM Micrsoft.Crm.Sdk and , Micrsoft.Crm.Sdk.TypeProxy dlls in your project.

Rename the class to a suitable name and add the lines
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;

Add a decorator [CrmWorkflowActivity("MyFirstWorkFlowActivity", "ConcatStrings")] to the class which will determine how it appears to the user in CRM and add the the method below

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) {return base.Execute(executionContext); }

Now sign the project at which point the bones of your workflow activity are in place.

In our example we will take two string parameters and and conconatate them into an ouput parameter add the code below. The decorator CrmInput and CrmOutput attributes determine which properties are visible in the CRM workflow editor. A property can be be set to both CrmInput and CrmOutput if required.

In our example we will take two string parameters and and conconatates them into an ouput parameter so add the code below

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
this.outString = this.inString1 + ", "+this.inString2 ;

return base.Execute(executionContext);

public static DependencyProperty inString1Property = DependencyProperty.Register("inString1", typeof(string), typeof(MyFirstCRMActivity));


public string inString1
get { return (string)base.GetValue(inString1Property);}
set { base.SetValue(inString1Property, value);}

public static DependencyProperty inString2Property = DependencyProperty.Register("inString2", typeof(string), typeof(MyFirstCRMActivity));

public string inString2
get { return (string)base.GetValue(inString2Property);}
set { base.SetValue(inString2Property, value); }

public static DependencyProperty outStringProperty = DependencyProperty.Register("outString", typeof(string), typeof(MyFirstCRMActivity));
public string outString
get { return (string)base.GetValue(outStringProperty);}
set { base.SetValue(outStringProperty, value); }

Build the project and copy the dll and pbl file to the C:\Program Files\Microsoft Dynamics CRM Server\Server\bin\assembly (assuming default install)

You now need to regsiter the DLL however do not use the registration tool that comes with the SDK use the version from

Once registered you can create a workflow in CRM which calls the new created dll
setting the input parameters in the first step and the output in the second


Gotchas - Here are a few problems you may come across

If you cannot publish your Workflow from CRM turn tracing on and check the trace logs

Check if namespace and class the same as this will prevent the workflow from been published

ErrorCode: -2147201023 - the type in the dependency property is not correct

When redeploying you may need to stop and restart IIS and the CRM Asynch service so you can use a batch file or build script to execute the following
net stop w3svc
net start w3svc
net stop MSCRMAsyncService
net start MSCRMAsyncService

Another common problem

Could not create activity of type 'CorrespondenceWorkflowActivity.Correspondence'. System.ArgumentException: Type 'CorrespondenceWorkflowActivity.Correspondence' does not define a static dependency property with name 'inGuidProperty'.

1 comment:

  1. Anonymous4:15 PM

    Thanks for the sharing of such information.This is a great stuff of reading. I will pass it on to our audience. Thanking you,CRM Services