Writing a simple custom workflow activity in CRM 4.0


Let’s create our first Custom Workflow Activity for CRM 4.0.

Check this post as well

https://nishantrana.wordpress.com/2008/10/30/custom-workflow-activities-in-crm-40/

We’ll take here a very simple scenario just to understand how to write custom activity.

This Custom Activity will take as input First Name and Last Name from an entity ( say Lead) and will update it’s description attribute with (First Name + Last Name).

Create a new Workflow Activity Library within workflow project type in Visual Studio.

(Keep the version as 3.0)

Name it MyFirstCustomWFlowActivity

Add references to Microsoft.crm.sdk.dll and Microsoft.crm.sdktypeproxy.dll.

Add the following line of code in your Activity1.cs.

using Microsoft.Crm.Workflow;

using Microsoft.Crm.Sdk;

We are creating a simple activity, so here we can have our class inherit from Activity class instead of SequenceActivity.

Delete the following class Activity1.Designer.cs as our simple custom activity doesn’t require that.


Decorate the class with CrmWorkflowActivity attribute

The first parameter is the user friendly name of the custom activity that will appear in the Workflow Editor and second parameter is the name of group, it can be used for grouping similar custom workflow activity.

namespace MyFirstCustomWFlowActivity

{

[CrmWorkflowActivity(“Get First and Last Name”, “Custom Steps”)]

public class Activity1: Activity

{

}

}

Now create three dependency properties, two as input for first name and last name and one as output where we will have full name i.e. first name and last name combined.

To create dependency property

Right click inside the visual studio editor then select insert snippet and followed by Workflow then DependencyProperty – Property.

We have used CrmInput and CrmOutput Attribute to make the dependency property visible within the workflow editor and to provide a user friendly name to them.

public static DependencyProperty FirstNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“FirstName”, typeof(string), typeof(Activity1));

[CrmInput(“First Name”)]

public string FirstName

{

get

{

return ((string)(base.GetValue(Activity1.FirstNameProperty)));

}

set

{

base.SetValue(Activity1.FirstNameProperty, value);

}

}

public static DependencyProperty LastNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“LastName”, typeof(string), typeof(Activity1));

[CrmInput(“Last Name”)]

public string LastName

{

get

{

return ((string)(base.GetValue(Activity1.LastNameProperty)));

}

set

{

base.SetValue(Activity1.LastNameProperty, value);

}

}

public static DependencyProperty FullNameProperty = System.Workflow.ComponentModel.DependencyProperty.Register(“FullName”, typeof(string), typeof(Activity1));

[CrmOutput(“Full Name”)]

public string FullName

{

get

{

return ((string)(base.GetValue(Activity1.FullNameProperty)));

}

set

{

base.SetValue(Activity1.FullNameProperty, value);

} }

Now comes the main part wherein we will override the Execute method and place our custom code to combine the first name and second name.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

this.FullName = this.FirstName + this.LastName;

return ActivityExecutionStatus.Closed;

}

Now Sign the assembly

Right Click the project in Solution Explorer

Select Properties

Select Signing

Check Sign The Assembly Check box and Select an existing or new file.

Now build the assesbly.

Now we’ll register the assembly

Open the plugin registration tool

Select Register-Register New Assembly and Select the custom workflow assembly there (MyFirstCustomWFlowActivity.dll)

Click on Register Selected Plugins button to register it in the database.

Now open CRM and go to settings then workflow and create a new workflow there.

In Workflow editor you can see our custom steps group at Add Step.

Now you can proceed as you would with other workflow and add your business logic around the custom workflow activity.

Within Add Step you would be able to set value for first name and last name property within the form assistant panel.

And than we can add an Update step wherein we can get the fullname output property within local values section inside form assistant panel.

Bye..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

2 thoughts on “Writing a simple custom workflow activity in CRM 4.0”

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.