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.
    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.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

       //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() + "");
About the Author

Developer, Designer, Thinker, Problem Solver, Office Servers and Services MVP, & Collaboration Director @ SoHo Dragon.

View Articles