Tuesday, August 6, 2013

Start SharePoint 2013 Workflow Programmatically

If you want to start workflow from server-side code, you need to use Microsoft.SharePoint.WorkflowServicesBase DLL.
If you don't see it in reference search, you can find it in GAC C:\Windows\Assembly with standard windows explorer search (of course if you have installed Workflow Manager).
And then to start a workflow:

 protected void StartWorkflow(SPWeb web, Guid workflowID, int itemID)  
 {  
   var wfsrvmng = new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager(web);  
   var wfsubsrv = wfsrvmng.GetWorkflowSubscriptionService();  
   var wfsub = wfsubsrv.GetSubscription(workflowID);  
   var wfi = wfsrvmng.GetWorkflowInstanceService();  
   wfi.StartWorkflowOnListItem(wfsub, itemID, new Dictionary<string, object>());  
 }  

where workflowID you can find in the workflow Elements.xml

 <Property Name="WSGUID" Value="..." />