Starting SharePoint Site Workflows from code behind

One of the cool new features of  SharePoint 2010 is Site Workflows. As you know, in MOSS 2007 a workflow can only be associated with a list or a document library. While this functionality is useful, sometimes a “site wide” workflow is called for. This leads to us developers getting creative and producing  approaches such as creating a list called “Site Workflows” and creating items in it that have “start on create” workflows attached to them. In SharePoint 2010 Site Workflows provide an elegant solution for this common scenario.

The Code

 using (SPSite site = new SPSite("http://sitename")) //get the site

           using (SPWeb web = site.OpenWeb()) // get the web
                        //find workflow to start
                        var assoc = web.WorkflowAssociations.GetAssociationByName("Workflow Name", CultureInfo.InvariantCulture);

                        //this is the call to start the workflow
                        var result = site.WorkflowManager.StartWorkflow(null, assoc, string.Empty, SPWorkflowRunOptions.Synchronous);


The Helper Class Code to call to start a list workflow

public void runWorkFlows(string strListName, int intCurrentItemID, string strWorkflowName)

using (SPSite objSite = new SPSite(SPContext.Current.Site.Url))
objSite.AllowUnsafeUpdates = true;
using (SPWeb objWeb = objSite.OpenWeb())
objWeb.AllowUnsafeUpdates = true;
SPList list = objWeb.Lists[strListName];
SPListItem lstItem = list.GetItemById(intCurrentItemID);

//obtain an instance of SPWorkflowManager which will be used to start the workflow
SPWorkflowManager manager = objSite.WorkflowManager;

//get all workflows associated with the list
SPWorkflowAssociationCollection associationCol = list.WorkflowAssociations;

//iterate through all the workflow and lookup for the workflow to be started

foreach (SPWorkflowAssociation association in associationCol)
if (association.Name == strWorkflowName)
//get workflow association data
string assData = association.AssociationData;

//start the workflow
manager.StartWorkflow(lstItem, association, assData);

objWeb.AllowUnsafeUpdates = false;
objSite.AllowUnsafeUpdates = false;

As you have noticed, the API call to start a site workflow is not much different form the call to start a list workflow. A new overload was added to SPWorkflowManager.StartWorkflow() method. This is the method we call to programmatically start our site workflow. One new parameter of this overloaded method which deserves a mention is the runOptions parameter, which expects a SPWorkflowRunOptions enumerator. See MSDN page for details.

One thought on “Starting SharePoint Site Workflows from code behind

Leave a Reply

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

You are commenting using your 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.