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

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.