views:

2845

answers:

4

Inside a workflow I want to handle and error such as not being able to lookup a username that I want to assign a task to. So the username doesn't exsist, I'm going to notify an administrator by email of this, log it to the workflow history and then terminate the workflow.

Question is, how do I terminate the workflow, from inside the workflow as if I was clicking the 'terminate workflow' button on the SharePoint webpage.

[Update] I've tried SPWorkflowManager.CancelWorkflow() which does indeed cancel the workflow but not immediately. What happens is the code to cancel runs but then my workflow continues on to create the next task and then goes to sleep when it hit's the next tasks onTaskChanged activity. Only once it has gone to sleep does the workflow get terminated, not when CancelWorkflow is called.

This causes the obvious problem that I don't want the next task to be created. I'm calling CancelWorkflow because I want it to cancel then and there.

+5  A: 

There are quite a few suggestions at this MSDN Thread:

Terminating a SharePoint Workflow Programatically

Here's a blog-post that succintly contains the exact same information: Cancelling a SharePoint Workflow

Lastly, and most specifically, you need to use the static method: SPWorkflowManager.CancelWorkflow(SPWorkflow workflowInstanceToBeCancelled)

EDIT

CancelWorkflow is a static class, so I've amended the call.

I've tried this but it hasn't solved my problem. I've updated the main post to reflect this.
Dan Revell
If anyone else tries SPWorkflowManager.CancelWorkflow() you might miss it the first time. The method is static so you want to call it on the class itself and not the workflow manager object that can be gotten from the SPSite like I tried.
Dan Revell
But it works correctly when you call it correctly, right? That is, when you call it on the class itself SPWorkflowManager.CancelWorkflow(SPWorkflow workflowInstance); then it does what it is supposed to do.
Yea it works fine, but it doesn't cancel the workflow when you call it, but it must flag the workflow to be cancelled once it's stoped executing. Aka when it goes to sleep waiting for an event. I want it to cancel as soon as it is called
Dan Revell
Can't you then toss the workflow to sleep from the catch block?
I've been looking for this for a while, and StackOverflow came thru for me. Thanks @DevinB
Chris Ballance
A: 

I'm not sure if this is the best way, but I just catch the error, log it, and then re-throw it. This results in an "Error Occurred" state in the list and immediately stops the workflow.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext provider)
{
    try
    {
        // do work
        // ...
    }
    catch (Exception e)
    {
        // log the error to the history list
        // ...
        throw;
    }

    return ActivityExecutionStatus.Closed;
}
Kit Menke
A: 

What is wrong with the terminate workflow activity?
There are a million ways to skin this particular cat.... however, what I'd like to know is how to cancel all of the tasks, and terminate the workflow while keeping the tasks around with a status of "cancelled by workflow", and then update the workflow status on the list item to "Cancelled".... I'm working on that right now... very hairy.

RKDMC
A: 

i think this would help u ! http://www.sharepointwithattitude.com/archives/68

mostafa