Home > SharePoint, SharePoint 2010 > SharePoint – Programatically Starting a Workflow

SharePoint – Programatically Starting a Workflow

Here is a code snippet I recently developed for starting a sharepoint workflow from code behind.

public void StartWorkFlow(string listName, int itemID, string workflowName)
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                oWeb.AllowUnsafeUpdates = true;
                SPList oList = oWeb.Lists[listName];
                SPListItem oItem = list.GetItemById(itemID);
                SPWorkflowManager wfManager = oSite.WorkflowManager;

                foreach (SPWorkflowAssociation association in oList.WorkflowAssociations)
                {
                    if (association.Name == workflowName)
                    {
                        string associationData = association.AssociationData;
                        wfManager.StartWorkflow(oItem, association, associationData);                                
                        break;
                    }
                }
                oWeb.AllowUnsafeUpdates = false;
            }
        }
    });
}
Advertisements
Categories: SharePoint, SharePoint 2010 Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

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: