简体   繁体   English

以编程方式取消SharePoint工作流

[英]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. 问题是,如何从工作流内部终止工作流,就像我单击SharePoint网页上的“终止工作流”按钮一样。

[Update] I've tried SPWorkflowManager.CancelWorkflow() which does indeed cancel the workflow but not immediately. [更新]我尝试过SPWorkflowManager.CancelWorkflow()确实取消了工作流程,但没有立即取消。 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. 取消运行的代码会发生什么,但随后我的工作流程继续创建下一个任务,然后在下一个任务onTaskChanged活动时进入休眠状态。 Only once it has gone to sleep does the workflow get terminated, not when CancelWorkflow is called. 只有一旦它进入睡眠状态,工作流程才会终止,而不是在调用CancelWorkflow时。

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. 我正在调用CancelWorkflow,因为我希望它取消当时和那里。

There are quite a few suggestions at this MSDN Thread: 这个MSDN线程有很多建议:

Terminating a SharePoint Workflow Programatically 以编程方式终止SharePoint工作流

Here's a blog-post that succintly contains the exact same information: Cancelling a SharePoint Workflow 这是一篇博客文章,其中包含完全相同的信息: 取消SharePoint工作流程

Lastly, and most specifically, you need to use the static method: SPWorkflowManager.CancelWorkflow(SPWorkflow workflowInstanceToBeCancelled) 最后,最具体地说,您需要使用静态方法: SPWorkflowManager.CancelWorkflow(SPWorkflow workflowInstanceToBeCancelled)

EDIT 编辑

CancelWorkflow is a static class, so I've amended the call. CancelWorkflow是一个静态类,所以我修改了调用。

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. 这会导致列表中出现“Error Error”状态,并立即停止工作流程。

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

    return ActivityExecutionStatus.Closed;
}

This is pretty old question, but to cancel workflow from itself right away is easy using this piece of code: 这是一个非常古老的问题,但是使用这段代码可以很容易地立即取消工作流程:

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

workflowProperties in this example is 此示例中的workflowProperties是

public SPWorkflowActivationProperties workflowProperties = 
           new SPWorkflowActivationProperties();

CancelWorkflow method will flag workflow as "canceling", but will not stop it until pause (like waiting for ontaskchanged event) or exception in the workflow. CancelWorkflow方法将工作流标记为“取消”,但在暂停(例如等待ontaskchanged事件)或工作流中的异常之前不会停止它。 So next statement of throw new Exception() will stop workflow and set it's status "Canceled". 所以抛出new Exception()的下一个语句将停止工作流并将其状态设置为“Canceled”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM