Friday, March 5, 2010

Creating custom activity in Workflow Part – 1

Hi All,

After long time, I am back to the workflow and really I have spent many months without writing anything related to the workflow.

See Creating custom activity in Workflow Part – 2 for further reading after reading this post.

This post will talk about creating custom activity in workflow. Is this the right time to talk about this topic, when you have bunch of articles already written on the net and books. Well, I think almost all of them starts with complex example of creating custom activity. Even in some books, they start with complex example, never written simple examples.

So I thought of giving a change to myself to write down very basic stuff and example about custom activity.

Custom activity in workflow is just like the concept of creating custom control for .Net applications. It can be user controls or a custom control. In the same way, in workflow world it can be single activity (Basic activity) or composite activity. Creating single activity is somewhat easier than creating composite activity. For example, Delay is basic activity and While is a composite activity.

Other one important point to remember is basic activity derives from System.Workflow.ComponentModel.Activity and composite activity is derived from System.Workflow.ComponentModel.CompositeActivity.

An activity is made up of four components. All are not required, only definition is required for an activity. However definitely we want to execute something in that, hence executor is also important.

1) Definition: This is the definition of an activity where we define properties, events etc.
2) Validator : This is for validating input properties value.
3) Executor: Execute the actual code in the activity.
4) Toolbox: This will help us to assign default values to properties at the time of dragging on Visual Studio designer surface.
5) Designer: This is about makeup of the activity. (You will say, yes definitely a makeup :) )

We will see each one of them in different parts. We will start with part 1 in this post and continue to explore each option in series.

So let us go ahead and start writing our basic code to create custom activity.

First open visual studio and choose workflow under C# and then choose custom activity. Add a class and perform the following steps. As we are creating basic activity, we will inherit from Activity class.

Make sure that your class is marked public; otherwise you will not be able to place activity in toolbox itself.

We have to create one dependency property and we are going to create BirthDate property here and our basic activity will return the age of a person.

Then we will override the Execute method and execute my logic and then we will return our ActivityExecutionStatus, that means if it succeeds, we will return closed and if not, we have to return Faulting.

Now go ahead and add one more sequential console workflow project to existing solution, and click on choose items on toolbox, browse to the DLL of just created custom activity and there you go, you will have it in the toolbox, just drag and drop it on the surface. Enter the BirthDate in BirthDate property, we’ve created.

Run it, Debug it, try it and see for yourself.

1 comment:

Anonymous said...

Awesome post.

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to