SharePoint - Programatically Starting a Workflow
Start a SharePoint 2010 list workflow from server-side code by finding its association by name.
Here is a snippet for starting a SharePoint workflow from code behind. It takes the list name, the item ID and the workflow association name, finds the matching association on the list and starts it for that item.
csharp
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 = oList.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;
}
}
});
}
Example
csharp
StartWorkFlow("Invoices", 12, "Invoice Approval");
This starts the "Invoice Approval" workflow on item 12 of the Invoices list.