Question

I have created one SharePoint 2013 designer workflow and it will run daily basis(Using : pause until Date).I want to "End the workflow" for the particular item by using Rest call or JSOM. How can I stop through the SharePoint rest API call?

anyone suggest the idea.

Was it helpful?

Solution

If you want to use JavaScript to stop workflow for item, we can use JSOM code to achieve it, the following function for your reference.

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.toLowerCase()) {
                    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());
        }
    );
};

More information is here: Workflow Services CSOM and JSOM API components

We can also use PowerShell with CSOM to achieve it.

Stop/Start workflow for SharePoint Online using PowerShell Script

OTHER TIPS

There is no REST API available to terminate workflows unless you write an API using server code, host and use it in your scripts.

Also, if the workflow has to be terminated using external code then it is supposed to be badly written workflow. There is Rest API to start the workflow and get instances but not to terminate a workflow as shown here.

I would advice you to rethink the logic and write the workflow such that it goes to the "End Workflow" stage based on the logic. Only that way will the workflow status show "Completed" and not "terminated".

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top