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

up vote 2 down vote accepted

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);
}
  • Hi Andrew, sorry I missed this reply previously. Thank you very much!! – Celbester Oct 30 '13 at 13:28

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.

  • Hi Mikhail, yes please. Or actually even just online references (if any) will be of big help already. Batiste at social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/… has suggested looking at the WorkflowInstanceService class. – Celbester May 29 '13 at 13:45
  • Hi Celbester. I had a lot of work and have completely forgotten that wanted to help you. Sorry for that! Is the question still actual? – MikhailSP Jun 13 '13 at 13:39

A second way is to use the workflow client via this blog post:

http://blogs.architectingconnectedsystems.com/blogs/cjg/archive/2013/12/03/HowTo_3A00_-Terminate-all-SharePoint-2013-workflows.aspx

  • 1
    Can you please extend your answer with more details, if the link breaks your answer will not be very useful right now :) – Robert Lindgren Dec 4 '13 at 18:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.