Trigger SP2010 Workflows using JSOM
Anatoly Mironov
Today I found out how to start workflows in JSOM (JavaScript Object Model in SharePoint). Nothing special, but since it is not documented, it took me a while to find a solution. Here is the code which I want to keep as simple as possible. What you need to start a SP2010 Workflow for a list item or a document in JSOM, you need to load SP.WorkflowServices.js and you need to create the manager and get the service, then you can trigger a workflow using the workflow name, the list guid and the guid of the list item:
var ctx = SP.ClientContext.get_current();
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx,
ctx.get_web());
var service = workflowServicesManager.getWorkflowInteropService();
service.startWorkflow(workflowName, null,
listGuid, plainItem.guid, initiationParams);Here is the code to trigger a workflow for multiple items:
//fire the workflows
function fire2010WorkflowForListItems(ctx,
listGuid, plainItems) {
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx,
ctx.get_web());
var service = workflowServicesManager.getWorkflowInteropService();
for (var i = 0; i < plainItems.length; i++) {
var plainItem = plainItems[i];
console.log('scheduling workflow for id: ',
plainItem.id);
service.startWorkflow(options.workflowName,
null, listGuid, plainItem.guid, options.initiationParams);
}
console.log('now executing...');
ctx.executeQueryAsync(function () {
console.info('yes, workflows completed for ' + items.length + ' items');
}, function () {
console.error('it didnt go well');
});
}The code above is inspired from this gist and sharepoint stackexchange. It is a simplified version that only works for list item workflows and SharePoint 2010 workflows. Here is an example how you can get multiple items and batch start a workflow:
//just a couple of variables
var options = {
workflowName: 'Behörigheter',
listName: 'Documents',
initiationParams: {}
};
//load list items
function startWorfklows() {
//Start 2010 Workflow for a List Item
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var lists = web.get_lists();
var list = lists.getByTitle(options.listName);
ctx.load(list);
var items = list.getItems(new SP.CamlQuery());
ctx.load(items);
ctx.executeQueryAsync(function () {
var listGuid = list.get_id() + '';
var en = items.getEnumerator();
var plainItems = [];
while (en.moveNext()) {
var it = en.get_current();
//do not take checked out files, it won't work
if (!it.get_item('CheckoutUser')) {
plainItems.push({
id: it.get_id(),
guid: it.get_item('GUID') + ''
});
}
}
fire2010WorkflowForListItems(ctx, listGuid,
plainItems);
}, function () {
alert('boom');
});
}
//Load Worfklow Js dependency
var wfScript = 'SP.WorkflowServices.js'
SP.SOD.registerSod(wfScript, _spPageContextInfo.webAbsoluteUrl
+ '/_layouts/15/SP.WorkflowServices.js');
SP.SOD.executeFunc(wfScript, '', startWorfklows);Why I needed it
I created a simple workflow that is triggered on Item Added and Item Updated. Unfortunately there are already thousands of items in the document library. To trigger them manually is nothing good. But a simple javascript solution did exactly what I wanted.
Comments from Wordpress.com
Anatoly Mironov - Oct 3, 2016
As far as I know, there is no js api for Workflows in SP2010. :(
Hi, Anatoly! great post! It’s really work on SP2010? Could you help me? I even can’t find and load SP.WorkflowServices.js (( Is there it in free access? Thank’s!