C# 以编程方式取消 SharePoint 工作流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1048981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically Cancel a SharePoint Workflow
提问by Dan Revell
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. 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.
[更新] 我试过 SPWorkflowManager.CancelWorkflow() 确实取消了工作流程,但不是立即取消。发生的是取消运行的代码,但随后我的工作流继续创建下一个任务,然后在点击下一个任务 onTaskChanged 活动时进入睡眠状态。只有一旦它进入睡眠状态,工作流才会终止,而不是在调用 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 是因为我希望它当时和那里取消。
回答by DevinB
There are quite a few suggestions at this MSDN Thread:
这个 MSDN Thread 有很多建议:
Terminating a SharePoint Workflow Programatically
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 是一个静态类,所以我修改了调用。
回答by Kit Menke
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;
}
回答by pirmas naujas
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
本例中的工作流属性是
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. So next statement of throw new Exception() will stop workflow and set it's status "Canceled".
CancelWorkflow 方法将工作流标记为“正在取消”,但不会停止它直到暂停(如等待 ontaskchanged 事件)或工作流中的异常。因此,下一条 throw new Exception() 语句将停止工作流并将其状态设置为“已取消”。