In SharePoint 2013, you can start a workflow with JavaScript Client Object Model. Basically SharePoint use JavaScript in out-of-the-box ‘start workflow’ pages. If you develop a new SharePoint 2013 workflow in Visual Studio and add a Initiation form, you will find JavaScript functions added to the Initiation Form to start workflow. The source code I’ve provided below is taken from the Initiation Form and presented with modification to work independently.
//dialog element to show during processing var dlg = null; //Subscription id - Workflow subscription id //list item id for which to start workflow. If site workflow, then send null for itemId function StartWorkflow(subscriptionId, itemId) { showInProgressDialog(); var ctx = SP.ClientContext.get_current(); var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()); var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId); ctx.load(subscription, 'PropertyDefinitions'); ctx.executeQueryAsync( function (sender, args) { var params= new Object(); //Find initiation data to be passed to workflow. var formData = subscription.get_propertyDefinitions()["FormData"]; if (formData != null && formData != 'undefined' && formData != "") { var assocParams = formData.split(";#"); for (var i = 0; i < assocParams.length; i++) { params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]]; } } if (itemId) { wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params); } else { wfManager.getWorkflowInstanceService().startWorkflow(subscription, params); } ctx.executeQueryAsync( function (sender, args) { closeInProgressDialog(); }, function (sender, args) { closeInProgressDialog(); alert('Failed to run workflow'); } ); }, function (sender, args) { closeInProgressDialog(); alert('Failed to run workflow'); } ); }
function closeInProgressDialog() { if (dlg != null) { dlg.close(); } } function showInProgressDialog() { if (dlg == null) { dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait...", "Waiting for workflow...", null, null); } }
Code Snippet 1: Start workflow
As shown in the above code example, you need to pass subscription id and item id. Everytime you associate workflow with list or site, you will get an association id for this association.To get an association id, go to the workflow settings page and then right click on workflow name and from the URL, you can find the subscription id as shown below:
Figure 1: Get the Workflow Association Id from Workflow Settings Page
If your workflow is Site Workflow (for your information, SharePoint 2013 workflow can run on a list item or for site), then pass null value for itemId. ‘FormData’ retrieved from workflow Subscription, will be passed to workflow initiation. But if you would like to pass your own additional parameters, you can do so as shown below:
var params= new Object(); //Find initiation data to be passed to workflow. var formData = subscription.get_propertyDefinitions()["FormData"]; if (formData != null && formData != 'undefined' && formData != "") { var assocParams = formData.split(";#"); for (var i = 0; i < assocParams.length; i++) { params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]]; } } //your custom parameters params['mykey']='this is custom value';
Code Snippet 2: Passing workflow initiation parameters
Similarly, if you would like to get JavaScript to associate workflow, you can add a workflow in Visual Studio and then add a association from. In the association form, you will find related JavaScript to associate workflow using JavaScript Object Model.
Finally you can all the javascript function provided in code snippet 1, as shown below (subscription id with curly braces):
StartWorkflow(‘{guid}’, itemId)