How to start SharePoint Designer 2013 Workflow using Javascript?
1 Answer
SharePoint 2013 provides set of JavaScript libraries that can be used to manage workflows. First, you need to refer sp.workflowservices.js which is available in _layouts/15/sp.workflowservices.js.
<script src="/_layouts/15/sp.workflowservices.js"></script>
you can use the below code to start your list workflow(s)
function startWF(itemID, listId) {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var workflowServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);
//run all workflows associated with the list
context.load(subscription);
context.executeQueryAsync(
function (sender, args) {
foreach(var workflowSubscription in subscriptions)
{
var inputParameters = {};
workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
}
context.executeQueryAsync(Function.createDelegate(this, this.onStartSucceeded),
Function.createDelegate(this, this.onStartFailed));
},
Function.createDelegate(this, this.onLoadFailed)
);
}
function onStartSucceeded(sender, args) {
alert("Workflow Started Successfully.");
}
function onStartFailed(sender, args) {
alert('Workflow Initiation Failed ' + args.get_message() +
'\n' + args.get_stackTrace());
}
function onLoadFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}