The following code snippet shows how to start a SharePoint 2013 workflow using JavaScript.

  • SharePoint 2013 workflows are substantially different than previous SharePoint 2010 workflows. You can read about the SharePoint 2013 architecture here.
  • The code is actually very similar to the C# code we would use to start the workflow. Here we are using CSOM to call out to the workflow subscription service, find the workflow associated with our list, and start it.
  • The sample code assumes you already have a workflow associated with the list.
  • The sample code contains the stubs for error handling. You’ll want to finish it yourself.
  • 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.
function StartWorkflow(listName, listGuid, itemId, workflowName, initiationParameters, callback) {     var clientContext = new SP.ClientContext.get_current();     var web = clientContext.get_web();     var list = web.get_lists().getByTitle(listName);     var item = list.getItemById(itemId);     clientContext.load(web);     clientContext.load(list);     clientContext.load(item);     var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, web);     clientContext.load(servicesManager);     var subs = servicesManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(listGuid);     clientContext.load(subs);     clientContext.executeQueryAsync(function () {         var subsEnum = subs.getEnumerator();         var workflowSub;         while (subsEnum.moveNext()) {             var sub = subsEnum.get_current();             var subName = sub.get_name();             if (subName == workflowName) {                 workflowSub = sub;                 break;             }         }         if (workflowSub) {             console.log('Web: ' + web.get_url() + ', Subscription: ' + workflowSub.get_name() + ', id: ' + workflowSub.get_id());             var initiationParams = initiationParameters || {};             servicesManager.getWorkflowInstanceService().startWorkflowOnListItem(workflowSub, itemId, initiationParams);             clientContext.executeQueryAsync(function (sender, args) {                 console.log('Workflow started.');             }, Function.createDelegate(this, logError));             callback();         }         else {             logError(this, {                 get_message: function () {                     return 'Can not find the workflow:' + workflowName;                 }             });         }     }, Function.createDelegate(this, logError));     function logError(sender, args) {         // Log error to console         console.log(args.get_message());         // Handle Errors     }; };

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.