Skip to content

Instantly share code, notes, and snippets.

@Aymkdn
Created October 11, 2012 15:22
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Aymkdn/3873181 to your computer and use it in GitHub Desktop.
Save Aymkdn/3873181 to your computer and use it in GitHub Desktop.
Start a workflow with Sharepoint (JavaScript)
/**
* Start a workflow
*
* @param {Object} params
* @param {String} params.listName The name of the list
* @param {Number} params.itemID The item ID
* @param {String} params.workflowName The name of the workflow
* @param {Array|Object} [params.parameters] An array of object with {Name:"Name of the parameter", Value:"Value of the parameter"}
* @param {Function} [params.after] Callback after the request is done
*/
function startWorkflow(params) {
// we need to make sure that SP.ClientContext is loaded
if (SP.ClientContext == undefined) {
setTimeout(function() { startWorkflow(params) }, 100);
return
}
params.after = params.after || (function() {});
if (!params.workflowName) { alert("Please provide the workflow name!"); return; }
function onQuerySucceeded() {
var enumerator = workflows.getEnumerator();
while (enumerator.moveNext()) {
var workflow = enumerator.get_current();
if (workflow.get_name() == params.workflowName) {
var url = 'http://' + window.location.hostname + item.get_item("FileRef");
var templateId = '{' + workflow.get_id().toString() + '}';
var workflowParameters = "<root />";
if (params.parameters) {
var p;
if (params.parameters.length == undefined) p = [ params.parameters ];
p = params.parameters.slice(0);
workflowParameters = "<Data>";
for (var i=0; i<p.length; i++)
workflowParameters += "<"+p[i].Name+">"+p[i].Value+"</"+p[i].Name+">";
workflowParameters += "</Data>";
}
// trigger the workflow
jQuery().SPServices({
operation:"StartWorkflow",
async:true,
item:url,
templateId:templateId,
workflowParameters:workflowParameters,
completefunc:params.after
});
break;
}
}
}
function onQueryFailed() { throw "Error with Start workflow" }
//var guid = new SP.Guid(__GlobalConfig.listID['Requested']);
var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
var list = lists.getByTitle(params.listName);
var item = list.getItemById(params.itemID);
var file = item.get_file();
context.load(list);
context.load(item);
var workflows = list.get_workflowAssociations();
context.load(workflows);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
@hajjaj
Copy link

hajjaj commented Apr 13, 2017

how to make this in office 365 , I have tired this but not working with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment