none
How to terminate SharePoint list workflow programmatically - in an event receiver? RRS feed

  • Question

  • Hi, I use SharePoint 2010.

    I have a list workflow, which I need to terminate if a particular column value changes to TRUE.
    I plan to do this in an event receiver with C# code. 
    How to detect the workflow and how to terminate it?

    Thanks so much in advance for your help.


    Friday, July 19, 2013 11:27 PM

Answers

  • Hi,

    I understand that you want to terminate a list workflow programmatically in an event receiver. You can create an itemupdated event handler. Try the sample code below:
    SPSecurity.RunWithElevatedPrivileges(delegate()
     { 
    using (SPSite site = new SPSite(properties.SiteId)) 
    using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
     {
     SPWorkflowManager manager = site.WorkflowManager;
     SPListItem item = web.Lists[properties.ListTitle].GetItemById(properties.ListItemId);
     foreach (SPWorkflow workflow in manager.GetItemActiveWorkflows(item))
     { 
    foreach (SPWorkflowTask t in workflow.Tasks)
     { 
    t["Status"] = "Canceled"; t.Update();
     }
     SPWorkflowManager.CancelWorkflow(workflow);
     }
     }
     } 
    });

    For more information, please refer to this site:

    How to cancel a workflow programmatically: http://www.brianfarnhill.com/2008/10/how-to-cancel-a-workflow-programmatically 

    Thanks,
    Entan Ming

    Entan Ming
    TechNet Community Support

    Sunday, July 21, 2013 6:11 AM
    Moderator