locked
Terminating a SharePoint Workflow Programatically

    Question

  •  

    I have a custome workflow coded in VS 2005 and deployed in to GAC and associated to a List in the SharePoint Portel through MS SharePoint Designer.

     

    When an Exception happened in the code, the worklow instance' Status is shown as "Error Occured" and get suspended. since this workflow instance is "hung up" at this list item another instance of the workflow can't run. To run another instance you have to go into the SP Portal --> List and click on the "Error Occured" under the workflow of the item and then you have to terminate the workflow by clicking on the "Terminate this workflow now" link.

     

    I just need to do this function ("Terminating the workflow") in the code in my Catch block where i handle the exception of the workflow activity.

     

    could some one please tell me how to do this? or even point me in the right direction?

     

    i've so far tried varity of:

    SPWorkflowManager.CancelWorkflow(workflow);

     

    and

     

    ActivityBind activityBind = new ActivityBind();

    new TerminateActivity(this.Name).SetBinding(TerminateActivity.ErrorProperty, ((System.Workflow.ComponentModel.ActivityBind)(activityBind)));

     

    and many more...

     

    none of them are terminating the workflow.

     

    please some one have any idea how to do this?

    Thursday, February 21, 2008 4:46 AM

Answers

  • Here is my code block to cancel the workflow and cycle throught its tasks.

     

     

    Code Snippet

    SPWorkflowCollection itemWorkflowCollection= listItem.Workflows;

    foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)

    {

    //cycle workflows associated to the item (listItem)

     

    if (!itemWorkflow.IsCompleted && itemWorkflow.InternalState == SPWorkflowState.Running)

    {

    foreach (SPWorkflowTask taskWorkflow in itemWorkflow.Tasks)

    {

    //cycle throught all tasks associated to the workflow

     

    //if task is not completed

    if (taskWorkflow["PercentComplete"].ToString() != "1")

    {

     

    //you can cancel or change the running tasks before canceling the workflow

    taskWorkflow["Status"] = "Canceled";

    taskWorkflow["PercentComplete"] = 1;

    web.AllowUnsafeUpdates = true;

    taskWorkflow.Update();

    }

    }

    SPWorkflowManager.CancelWorkflow(itemWorkflow);

    }

    }

     

     

    Hope this helps

     

    André

     

     

    • Proposed as answer by Oisin Grehan Wednesday, November 05, 2008 5:06 PM
    • Marked as answer by Mike Walsh FIN Saturday, May 09, 2009 7:50 AM
    Monday, July 21, 2008 2:24 PM
  • Hello:

    I´v got a solution and it works

    so, you have to write the whole code within

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

    //your code

    }

    that way everything is done with admin account. you must include the whole code

    since you get the item from the list until you call Manager.CancelWorkflow()

    all those operations run without troubles because the permissions are gotten with admin. the most common trouble that i got while trying to cancel a WF was that I was getting the item out of the

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {}

    and then I called the manager.cancelWorkflow() within  

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {}

    that was the main reason it does not work properly.

    I hope this help you.

    greetings

    steve

    • Marked as answer by Mike Walsh FIN Sunday, December 26, 2010 10:34 AM
    Wednesday, August 25, 2010 9:51 PM

All replies

  •  

    I have been pulling my hair out over this one as well...

     

    I have tried the static method

    SPWorkflowManager.CancelWorkflow(workflow);

     

    as well as the instance method;

     

    wfmgr.RemoveWorkflowFromListItem(workflow)

     

    All I get is the totally uniformative exception message "Value does not fall within the expected range."

    Starting workflows on the other hand works as you would expect.

     

    I wondered if anyone has had any luck. It does seem odd that within the default environment there is a no nonsense link that terminates workflow .If it can be done there you would have thought...

     

    Wednesday, July 02, 2008 4:57 AM
  • Hello,

     

    I have encountered a simmilar problem.

     

    I found that I was using the properties object inside the RunWithElevatedPrevileges and that would change the running user from the System Account to the current user (there is no error or exception informing that this happened or perhaps you can get permissions denied exceptions).

     

    I removed the call to the properties and it worked

     

    Hope this helps

    Regards,

     

    André

    Friday, July 18, 2008 11:24 AM
  •  

    Look into the permissions required to complete the action.  If you are doing it programatically, you might find that you need to run it under elevated permissions and when this is completed it is important to note that the context of the site / list needs to be regained before it will work.
    Friday, July 18, 2008 12:19 PM
  • The running workflows are associated with the SPListItem, so you should loop through the running workflows and remove the ones you do not want using, i have not tested this but something like this should work:

     

    foreach(SPWorkflow wf in MyItem.Workflows)

    {

    if(wf == "The one I want to remove")

    {

    myitem.Workflows.Remove(wf.ID);

    }

    }

    Friday, July 18, 2008 1:46 PM
  • Here is my code block to cancel the workflow and cycle throught its tasks.

     

     

    Code Snippet

    SPWorkflowCollection itemWorkflowCollection= listItem.Workflows;

    foreach (SPWorkflow itemWorkflow in itemWorkflowCollection)

    {

    //cycle workflows associated to the item (listItem)

     

    if (!itemWorkflow.IsCompleted && itemWorkflow.InternalState == SPWorkflowState.Running)

    {

    foreach (SPWorkflowTask taskWorkflow in itemWorkflow.Tasks)

    {

    //cycle throught all tasks associated to the workflow

     

    //if task is not completed

    if (taskWorkflow["PercentComplete"].ToString() != "1")

    {

     

    //you can cancel or change the running tasks before canceling the workflow

    taskWorkflow["Status"] = "Canceled";

    taskWorkflow["PercentComplete"] = 1;

    web.AllowUnsafeUpdates = true;

    taskWorkflow.Update();

    }

    }

    SPWorkflowManager.CancelWorkflow(itemWorkflow);

    }

    }

     

     

    Hope this helps

     

    André

     

     

    • Proposed as answer by Oisin Grehan Wednesday, November 05, 2008 5:06 PM
    • Marked as answer by Mike Walsh FIN Saturday, May 09, 2009 7:50 AM
    Monday, July 21, 2008 2:24 PM
  • If you cancel a workflow via the sharepoint GUI, all corresponding tasks are deleted.
    Does this also happen if you cancel it programmatically?

    Friday, December 12, 2008 9:49 AM
  • hello ,
    i have a same problem, have you got solution.
    thanks
    Monday, June 15, 2009 10:21 PM
  • The user has to have the ManageLists permission for CancelWorkflow to work. Otherwise you will get the error. Now if you can work out some code to give the user the permission temporarily to perform the cancel or just have people who will be cancelling the workflow set up with that right in the first place, you should not have any trouble.
    Friday, June 19, 2009 5:15 PM
  • Hello:

    I´v got a solution and it works

    so, you have to write the whole code within

     

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

    //your code

    }

    that way everything is done with admin account. you must include the whole code

    since you get the item from the list until you call Manager.CancelWorkflow()

    all those operations run without troubles because the permissions are gotten with admin. the most common trouble that i got while trying to cancel a WF was that I was getting the item out of the

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {}

    and then I called the manager.cancelWorkflow() within  

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {}

    that was the main reason it does not work properly.

    I hope this help you.

    greetings

    steve

    • Marked as answer by Mike Walsh FIN Sunday, December 26, 2010 10:34 AM
    Wednesday, August 25, 2010 9:51 PM