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)
{

SPSecurity.RunWithElevatedPrivileges(delegate()
{
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);

break;
}
}
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.

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

One Response to Starting SharePoint Site Workflows from code behind

  1. Can you please provide more details on SPWorkflowRunOptions

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: