Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Cancel a SharePoint Workflow

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.

like image 647
Daniel Revell Avatar asked Jun 26 '09 12:06

Daniel Revell


People also ask

How do I stop my workflow from running?

To stop a workflow run (console)In the navigation pane, under ETL, choose Workflows. Choose a running workflow, and then choose the History tab. Choose the workflow run, and then choose Stop run. The run status changes to Stopping.


1 Answers

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.

like image 193
DevinB Avatar answered Oct 13 '22 22:10

DevinB