Skip to content

Instantly share code, notes, and snippets.

Created March 24, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0d01db91dbb293f3940a4825e264d2a8 to your computer and use it in GitHub Desktop.
Save anonymous/0d01db91dbb293f3940a4825e264d2a8 to your computer and use it in GitHub Desktop.
Javascript Loading a workflow on column change
$(document).ready(function() {
document.getElementById("ctl00_ctl42_g_c46573bc_6049_45fc_a2f8_09c5196e69d1_ctl00_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem").addEventListener("click", function(){
var dateonsubmit = document.getElementById("DeliveryMonth_934d5445-bd6e-4fc9-a9e9-958e063dd203_$DateTimeFieldDate").value;
var datebeforesubmit = localStorage.getItem("initialDate");
if(datebeforesubmit != dateonsubmit){
var listItemId = getQueryStringParameter('ID');
var jqDeferred = $.ajax(startWorkflow(listItemId, 'ecb75086-2b01-4454-bdd3-1caeee89c62c'));
jqDeferred.then(function(response, statusText, xhrObj) {
alert("ok");
}, function(xhrObj, textStatus, err) {
alert("no");
})
}
});
var initialdate = document.getElementById("DeliveryMonth_934d5445-bd6e-4fc9-a9e9-958e063dd203_$DateTimeFieldDate").value;
localStorage.setItem("initialDate",initialdate);
});
function startWorkflow(itemID, subID) {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(subID);
context.load(subscription);
context.executeQueryAsync(
function(sender, args){
console.log("Subscription load success. Attempting to start workflow.");
var inputParameters = {};
wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemID, inputParameters);
context.executeQueryAsync(
function(sender, args){ alert("Starting workflow"); },
function(sender, args){
alert("Failed to start workflow.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
},
function(sender,args){
alert("Failed to load subscription.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
}
function getQueryStringParameter(p) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == p)
return singleParam[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment