How to programmatically stop workflows in Dynamics 2011?

dynamics-crm, dynamics-crm-2011

Solution

This comes via: http://kelvinshen.blogspot.com/2011/09/cancelling-workflows-via-sdk.html

Entity workflowToCancel = new Entity("asyncoperation");

workflowToCancel.Id = workflowID;
workflowToCancel["statecode"] = new OptionSetValue(3);
workflowToCancel["statuscode"] = new OptionSetValue(32);

myCRMService.Update(workflowToCancel);

Problem

I need to stop all running workflows associated to an incident (and those with a name that begins with "Auto_"). However, I can't seems to find any documentation on how to stop workflow programmatically and I can't find how they are stored in the DB.

Original source