0

I'm attempting to write a script that will cancel and start workflows when an error happens. I found this section of code on different sites but have not been able to get it to work. After stepping through the code I know the instancesEnum is empty. If it makes a difference, I'm working with SharePoint 2013 with a SharePoint 2010 workflow.

function test(){
var title; var workflow; var modified;
var listID = "672AF4F1-7160-4D93-B83B-2DA5C0F298B1";
var workflowID = "49DB5E25-E862-49DC-BE90-683B2AC9C6F8";


$().SPServices({
    operation: "GetListItems",
    listName: "My Testing",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            title = $(this).attr("ows_Title");
            workflow = $(this).attr("ows_TestCrea");
            modified = $(this).attr("ows_Modified");
            id =  parseInt($(this).attr("ows_ID"));
            if(workflow == 2){ //Using in progress instead of error for testing
                console.dir(title + " -" + workflow + " -" + modified + "  " + id);
                terminateWorkflow(listID,id ,workflowID);
            }
        });
    }
});

}

function terminateWorkflow(listId, itemId, subId){
var context = SP.ClientContext.get_current();
var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web()); 
var workflowInstanceService = workflowServicesManager.getWorkflowInstanceService();
var wfInstances = workflowInstanceService.enumerateInstancesForListItem(listId, itemId);
context.load(wfInstances);
context.executeQueryAsync(
    function(sender, args){
        var instancesEnum = wfInstances.getEnumerator();
        while(instancesEnum.moveNext()){
            var instance = instancesEnum.get_current();
            if(instance.get_workflowSubscriptionId().toString() == subId){
                workflowInstanceService.terminateWorkflow(instance);
                context.executeQueryAsync(
                    function(sender, args){
                        console.log("Termination Successful");
                    },
                    function(sender,args){
                        console.log("Failed to terminate workflow.");
                        console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
                    }
                );
            }
        }
    },
    function(sender,args){
        console.log("Failed to load instances.");
        console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
    }
);

};

2 Answers 2

0

Unfortunately 2010 Workflows do make a difference. Using "SP.WorkflowServices.WorkflowServicesManager" only works on SP2013 Workflows. From what I understand, there isn't a "Stop Workflow" web service we can hit with 2010 Workflows.

0
0

In my library I created a function that permits to stop a Workflow 2010 : https://aymkdn.github.io/SharepointPlus/lists.html#stopWorkflow

This technique uses an iframe to load the workflow page and trigger the "End the workflow" link, so you need to make sure the user has correct permissions, uses a browser and is on the same website (to avoid security errors).

That's the only way I found to stop a workflow 2010.

2
  • I have been attempting to use your library and I get the following error. Any ideas? "unable yo get property '__doPostBack' of undefined or null reference" Commented Apr 5, 2019 at 14:52
  • Open an issue on Github! Commented Apr 5, 2019 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.