Below is some sample code to delete a workflow on the host web from your app web. This would be useful in the event you want your App to remove a workflow that you have deployed to the host web.
NOTE: you must include a reference to “/_layouts/15/SP.WorkflowServices.js”
var deleteWorkflow = function(workflowName) { //Using the App Web as the client context clientContext = new SP.ClientContext.get_current(); //Get the host web URL from the query string params //I have a function getHostWebUrl() - which is not included. //http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript var hostWebUrl = getHostWebUrl(); //Using the hostWebContext as an AppContextSite hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl); //Get the Workflow Services Manager for the Host web, NOTE: you initialize it with the clientContext & the hostWebContext Web) var hostWebWorkflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, hostWebContext.get_web()); var hostWebWorkflowDeploymentService = hostWebWorkflowServicesManager.getWorkflowDeploymentService(); var hostWebDefinitionsCollection = hostWebWorkflowDeploymentService.enumerateDefinitions(false); //load all the workflow definitions from the host web clientContext.load(hostWebDefinitionsCollection); clientContext.executeQueryAsync(function() { //get the enumerator for all the workflow definitions var workflowDefinitions = hostWebDefinitionsCollection.getEnumerator(); while (workflowDefinitions.moveNext()) { //set the current definition to a variable var workflowDefinition = workflowDefinitions.get_current(); //uncomment to see all the workflows that it finds //console.log("Found Workflow : " + workflowDefinition.get_displayName()); //looking for a match on the name of the workflow if (workflowDefinition.get_displayName() === workflowName) { //using the host web workflow deployment service to delete the workflow definition hostWebWorkflowDeploymentService.deleteDefinition(workflowDefinition.get_id()); //this comment will just display what workflow WILL BE deleted console.log("Deleted workflow : " + workflowName + ", " + workflowDefinition.get_id().toString()); //if more than one workflow with the same name is found it will delete all of them //the delete will get queued up and executed by the .executeQueryAsync //if you don't want that then you have to break out of the while loop } } //this query will execute the delete action clientContext.executeQueryAsync(function() { //Successfully deleted the workflow }, function(sender, args) { console.log("Unable to delete workflow : " + workflowName); console.log("Reason : " + args.get_message() + ""); }); }, function(sender, args) { console.log("Failed to load workflows"); console.log("Reason : " + args.get_message() + ""); }); };
Hi Thomas. Thank you so much for the WF javascript. It took me whole day and once I used yours it worked like a charm. The issue I had was i was calling executequery on the hostwebcontext instead and once I use app web context to call it it worked.
Again Thanks for this nice article.
Arash
That’s why I posted it 🙂 I struggled for a day or so trying to figure out why I was getting access denied with the calls. The documentation on it is not 100% yet.
Glad this helped!