Yes, you can do it from JS. The code will be something like this:
(function () {
var workflowInstanceId = 'PUT-YOUR-GUID-HERE';
var context = SP.ClientContext.get_current();
var web = context.get_web();
var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var instance = servicesManager.getWorkflowInstanceService().getInstance(workflowInstanceId);
servicesManager.getWorkflowInstanceService().suspendInstance(instance);
// or servicesManager.getWorkflowInstanceService().resumeInstance(instance);
context.executeQueryAsync(function (sender, args) {
alert('Instance was suspended successfully!');
},
function (sender, args) {
alert("Error occured! " + args.get_message() +
'\r\nStack trace: ' + args.get_stackTrace());
});
})();
If you don't know the workflow instance ID, you can enumerate all the instances e.g. for a list item, using method enumerateInstancesForListItem(listId, itemId)
of the WorkflowInstanceService.
Also you can determine if the workflow is suspended or is running at the moment by getting the instance (don't forget context.load(instance)
, and then inside executeQueryAsync success callback you can use it) and then analysing the get_status()
property of the instance. Possible status values are stored in the SP.WorkflowServices.WorkflowStatus
enumeration.
Useful resources:
- Using SharePoint Workflow Services JS API (from CodeProject)
- Annotated definition for WorkflowServices.js SharePoint file
Disclaimer: both the article and the definition were authored by me.