Friday, September 5, 2008

How to attach workflow to list from code

Hi All,

There may be the case where you need to Attach the Workflow to a list dynamically depending on several conditions in the code.

so here is the way to get this thing done from code.

For this, First you need to install the Workflow as a feature and then keep it there only, i suggest to install this feature at site collection level, so that you get this for each and every site where you need it.

Let me explain you several things,

Each web is having several workflow templates within. These are the default workflows that you can find when you go to the list or document library settings -> workflow settings. These are the templates for the workflow already installed for you to use. so when you navigate through the code, you will get all these.

Let's perform each step one by one.

(1) Navigate through the WorkflowTemplate collection on web. for this you need to use SPWorkflowTemplate class. Consider currentWeb as web.

SPWorkflowTemplate baseTemplate = null;

currentWeb.AllowUnsafeUpdates = true;

for (int iCount = 0; iCount <= currentWeb.WorkflowTemplates.Count - 1; iCount++)
if (currentWeb.WorkflowTemplates[iCount].Name == "{workflow_name}")
baseTemplate = currentWeb.WorkflowTemplates[iCount];

Here just see that we have navigated through the workflow templates. Here you will find all default workflow names. Now as soon as you get your workflow name, you take that template in to the baseTemplate which is SPWorkflowTemplate object.

Now you have this object in your hand, you need to take two lists in your hand to get this work done.


SPList workflowhistory = currentWeb.Lists["Workflow History"];
SPList tasks = currentWeb.Lists["Tasks"];

These are the two lists which will be required when you will create the association with the list for workflow.

(3) Now create one association.

SPWorkflowAssociation newAssociation = null;

newAssociation = SPWorkflowAssociation.CreateListAssociation(baseTemplate,
"{Give name of the workflow}", tasks, workflowhistory);

As you can see, you have mentioned here the tasks list and workflowhistory list for this list.

(4) If you want to auto start this workflow at the time of new creating the item, set

newAssociation.AutoStartCreate = true;

Or if you want that when item is modified in the list, and at that time workflow triggers, set

newAssociation.AutoStartChange = true;

(5) Get the List object on which you want to attach the workflow.

SPList MyList = currentWeb.Lists["{listname}"];



currentWeb.AllowUnsafeUpdates = false;

That's it. your job is done.


Anonymous said...

Nice! and thank you!

Anonymous said...

Nice! and thank you!

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