Invoke Workflow Programmatically

on Thursday, August 13, 2009

Hi All,

Sometimes i wonder about the Workflow triggering technique from the List and Document Library. I wonder because of one observation.

Let me share it with you, when you attach the workflow to a list or document library we have an option to configure it that when to trigger, is it on adding of the new item or is it on Updating the item. Adding a new item is a one time job, so for this scenario, it looks perfect for me.

But, when i consider trigger when updating item, it is something that i do not think this is done properly.

Because Once you open the ListItem for edit, do not edit anything and press OK. What happens, Still Workflow Triggers.

Now the question is, is it really cool to trigger the workflow even if you have not made any changes???????? I do not think so...

So bottom line is to check the proper condition in code and then dynamically from code, trigger the workflow.

Here is a way to trigger the workflow from code.

First you need to take the SPWorkflowManager Object.

SPWorkflowManager objWorkflowManager = null;

Then use SPWorkflowAssociationCollection object. every List and Document library has association with the workflow, to get this we have to use this object to collect all workflows which are associated with the List or DocumentLibrary.

SPWorkflowAssociationCollection objWorkflowAssociationCollection = null;

I consider that i am using Event Handler, if you are using this code anywhere else, change the Web and Site objects accordingly.

We have WorkflowManager object at Site Level, so first we will take it.

objWorkflowManager = item.Web.Site.WorkflowManager;

Then we will take all association of the workflow for specific list.

objWorkflowAssociationCollection = item.ParentList.WorkflowAssociations;

Now consider a scenario, where you have multiple workflows associated with the same list or document library. So First we need to find the correct Workflow Association to trigger only that workflow.

So for that first Loop through all Associations,

foreach (SPWorkflowAssociation objWorkflowAssociation in objWorkflowAssociationCollection)

if (String.Compare(objWorkflowAssociation.BaseId.ToString("B"), {"Workflow_GUID"}, true) == 0)


//We found our workflow association that we want to trigger.

//Replace the workflow_GUID with the GUID of the workflow feature that you
//have deployed.

objWorkflowManager.StartWorkflow(item, objWorkflowAssociation, objWorkflowAssociation.AssociationData, true);
//The above line will start the workflow...

Sample Code:

using (SPSite site = new SPSite("http://localhost:777/"))
using (SPWeb web = site.OpenWeb())
SPWorkflowManager manager = site.WorkflowManager;
SPList InvoiceList = web.Lists["Invoices"];
SPWorkflowAssociation helloWorldAssociation = null;
SPListItem item = InvoiceList.Items[0];
foreach (SPWorkflowAssociation association in InvoiceList.WorkflowAssociations)
if (association.BaseId == new Guid("64A0BC39-E987-4c39-9308-15F6511E8435"))
manager.StartWorkflow(item, association,"",true);