Our custom code starts the workflow using classes in the Microsoft.SharePoint.WorkflowServices namespace but we can't seem to find a method to end the workflow.
Your help and time is greatly appreciated. Thank you. :)
I wouldn't use the server side OM (what you reference in your post), use the CSOM implementation as it's going to work both in an on-prem & Office 365 option. What you need to do is get a reference to the specific instance you want to work with.
You first need a reference to the instance service and then pass in the ID of the list & list item to see all instances, like this:
// connect to the workflow services via a CSOM client context
var clientContext = new ClientContext(siteCollectionUrl);
var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);
// connect to the instance service
var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();
// get all instances
var workflowInstances = workflowInstanceService.EnumerateInstancesForListItem(listId, listItemId);
clientContext.Load(workflowInstances);
clientContext.ExecuteQuery();
From there, you can enumerate each of them and find the one you want to suspend or terminate:
foreach (var instance in workflowInstances)
{
// is this what we are looking for?
// ...
// if so, suspend it
workflowInstanceService.SuspendWorkflow(instance);
// or you can terminate it
workflowInstanceService.TerminateWorkflow(instance);
}
I can remember that I did it some time before (where were possibilities not only to cancel workflow, but also to suspend and resume), but I could not find my solution for a while. If it is not so urgent, I can try to find some time tomorrow to do that again.
A second way is to use the workflow client via this blog post: