Skip to content

Instantly share code, notes, and snippets.

@iOnline247
Created February 19, 2014 19:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iOnline247/9099157 to your computer and use it in GitHub Desktop.
Save iOnline247/9099157 to your computer and use it in GitHub Desktop.
SPServices.StartWorkflow
$.fn.SPServices.StartWorkflow = function(options) {
var opt = $.extend({}, {
itemUrl: null,
workflowName: null,
workflowParameters: "<root />",
completefunc: null, // Function to call on completion of rendering the change.
debug: false // If true, show error messages;if false, run silent
}, options),
thisFunction = "SPServices.StartWorkflow",
executeWorkflow = $.Deferred(),
templates = null,
getTemplatesForItem = "GetTemplatesForItem"
;
function getTemplates() {
return $().SPServices({
operation: getTemplatesForItem,
item: opt.itemUrl
});
}
function startWorkflow(workflowTemplates) {
var workflowGUID = null,
$workflowTemplates = $(workflowTemplates),
startWorkflow = "StartWorkflow",
output = {}
;
output[getTemplatesForItem] = workflowTemplates;
$workflowTemplates.find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
var $this = $(this)
;
// find workflow name
if ($this.attr("Name") === opt.workflowName) {
var guid = $this.find("WorkflowTemplateIdSet").attr("TemplateId");
if (guid) {
workflowGUID = "{" + guid + "}";
output.workflowGUID = workflowGUID;
// Stops jQuery#each iteration
return false;
}
}
});
if (workflowGUID) {
$().SPServices({
operation: startWorkflow,
item: opt.itemUrl,
templateId: workflowGUID,
workflowParameters: opt.workflowParameters,
completefunc: function(data, status) {
output[startWorkflow] = data;
executeWorkflow.resolve(output);
if ($.isFunction(opt.completefunc)) {
opt.completefunc(output, status);
}
}
});
} else if (opt.debug) {
// create error message
var workflowError = workflowTemplates.status === 404 ?
"The item's URL was not found." :
"Workflow name not found."
;
errBox(thisFunction, "workflowName: " + opt.workflowName, workflowError);
executeWorkflow.reject(output);
} else {
// workflowGUID not defined or network error occured with the GetTemplatesForItem call.
executeWorkflow.reject(output);
}
}
// Fire Workflow
// jQuery#always is used here, so we guarantee execution of startWorkflow which resolves/rejects the promise.
getTemplates().always(startWorkflow);
return executeWorkflow.promise();
};
// Example Usage
var linkNavWrkFlow = $().SPServices.StartWorkflow({
debug: true,
itemUrl: "http://portal/SPDev/Lists/LinkNav/21_.000",
workflowName: "Test Wrkflw Params",
workflowParameters: "<Data><TestField>All up in your SharePoints.</TestField></Data>",
completefunc: function(data, status) {
// console.log("StartWorkflow Op Data: " + data.StartWorkflow.responseText);
console.log("Status: " + status);
}
});
// Chaining example
$.when( linkNavWrkFlow ).then(
function( linkNavWrkFlowData ) {
// console.log("StartWorkflow Op Data: " + linkNavWrkFlowData.StartWorkflow.responseText);
},
function( linkNavWrkFlowData ) {
// console.log("StartWorkflow Failed Op Data: " + linkNavWrkFlowData.StartWorkflow.responseText);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment