Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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);
}
Owner

Aymkdn commented Oct 11, 2012

Example:

startWorkflow({
  listName:"My List",
  itemID:258,
  workflowName:"My workflow",
  parameters:[{Name:"Param1", Value:"Something"}, {Name:"Param2", Value:"Something Else"}],
  after:function() { alert("OK") }
})

Do you know if you can start the workflow w/o using SPServices in SP 2010?

Owner

Aymkdn commented Jul 22, 2013

Github didn't notify me on this comment.... So it's why my answer is coming so late... So yes i think, look at http://www.codeproject.com/Articles/607127/Using-SharePoint-2013-Workflow-Services-JS-API#example5 (I guess it should work with SP2010 too...)

snugam commented Aug 13, 2014

Brilliant - THANK YOU!

@iOnline247. Have you tried this code with SharePoint 2010?

Thank you very much! Works good on SP2010.

SAFU29 commented Aug 4, 2016

@gendalf7771 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!

Englbach commented Nov 30, 2016

How to use it on sharepoint 2013? This is a sample

`<script src="../../SiteAssets/jquery.SPServices-2014.02.min.js" type="text/javascript"></script> 
<script src="../../SiteAssets/jquery.SPServices-2014.02.js" type="text/javascript"></script>
<script type="text/javascript">

 $("#btnSubmit").click(function() 
{ $().SPServices({ operation: "StartWorkflow",
 item:"../../Workflows/SubmitWF/SubmitWF.xoml",
 templateId:"{04ee1c93-f6b7-49b3-a79c-fa3142ecd688}", worflowParameters:"<root/>" }); }); 
</script>`

Fareedbaba commented Jan 17, 2017

Can you please help me by providing how can we the access the parameters passed in the above code example (params.parameters) in SPD 2010 or SPD 2013

bgericke commented Feb 9, 2017

Hey, @Aymkdn

I have a small improvement for your code:
Instead setting a timeout at the beginning, there's a sharepoint function that waits for scripts to be loaded. It's easy to use:

SP.SOD.executeOrDelayUntilScriptLoaded(function() { YOUR CODE HERE }, "sp.js");

This way your script waits until sp.js is loaded, which provides the SP.ClientContext for your async query.
I use this a lot in my SP scripts, it provides more performance than setting a timeout.

Thanks for sharing this snippet, it's very useful! :)

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