Programmatically Cancel SharePoint Workflow

Inside the workflow, I want to handle errors, for example, I am not able to find the name of the user to whom I want to assign a task. Thus, the username is not exsist, I am going to notify the administrator by email about this, register it in the history of the workflow and then complete the workflow.

The question is how can I complete the workflow from within the workflow, as if I clicked on the “end workflow” button on the SharePoint web page.

[Update] I tried SPWorkflowManager.CancelWorkflow (), which really cancels the workflow, but not right away. What happens is the code to cancel the launch, but then my workflow continues to create the next task, and then goes into sleep mode when it falls into the following onTaskChanged tasks. Only after he falls asleep does the workflow end, and not when CancelWorkflow is called.

This leads to an obvious problem that I do not want the next task to be created. I call CancelWorkflow because I want it to cancel it there too.

+7
c # workflow sharepoint
source share
3 answers

There are several suggestions in this MSDN release:

Automatically complete a SharePoint workflow

Here's a blog post that provides accurate information: Cancel a SharePoint workflow

Finally, and in particular, you need to use the static method: SPWorkflowManager.CancelWorkflow(SPWorkflow workflowInstanceToBeCancelled)

EDIT

CancelWorkflow is a static class, so I changed this call.

+9
source share

I'm not sure if this is the best way, but I just catch the error, register it and then throw it again. This leads to 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; } 
0
source share

This is a pretty old question, but canceling the workflow right away is easy with this piece of code:

 SPWorkflowManager.CancelWorkflow(workflowProperties.Workflow); throw new Exception(); 

workflowProperties in this example

 public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); 

The CancelWorkflow method will mark the workflow as "cancel", but will not stop it until a pause (for example, waiting for the ontaskchanged event) or an exception in the workflow. Therefore, the following throw new Exception () statement will stop the workflow and set its status to “Canceled”.

0
source share

All Articles