Starting a SharePoint 2013 workflow programmatically using C#

The following code snippet shows how to create a SharePoint 2013 workflow using C#. While the code itself is pretty self explanatory, here are some hints:

  • SharePoint 2013 workflows are substantially different than previous SharePoint 2010 workflows. You can read about the SharePoint 2013 architecture here. Therefore, the code to start them is different as well. 
  • When starting the workflow, you must tap into the workflow subscription service, find the workflow you want to start and start it.
  • The sample code assumes you already have a workflow associated with the list.
  • The sample code does not contain any error handling. 
  • The sample code shows how to add input parameters. If your workflow has an initiation form that passes values into the workflow, you can create those values here.

/// <summary> /// Starts the SharePoint 2013 workflow. /// </summary> /// <param name="item">The list item that we want to start the workflow against.</param> /// <param name="workflowName">The name of the workflow that is associated with the list.</param> private static void Start2013Workflow(SPListItem item, string workflowName) {     var workflowServiceManager = new WorkflowServicesManager(item.Web);     var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();     // Get all workflows associated with the list     var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(item.ParentList.ID);     // Run all workflows associated with the list     foreach (var workflowSubscription in subscriptions)     {         if (workflowSubscription.Name.Equals(workflowName))         {             // Pass in any initiation parameters             var inputParameters = new Dictionary<string, object>();             //inputParameters.Add("MyProperty", "MyValue");                          workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, item.ID, inputParameters);         }     } } 

Starting a SharePoint 2013 workflow programmatically using the JavaScript Client Object Model

What is the Office Graph and how can we use it?