在工作流程中,我想要处理错误,例如无法查找我想要为其分配任务的用户名。 因此该用户名不存在,我将通过电子邮件通知管理员,将其记录到工作流程历史记录中,然后终止工作流程。
问题是,如何从工作流内部终止工作流,就像单击 SharePoint 网页上的“终止工作流”按钮一样。
[更新]
我尝试过 SPWorkflowManager.CancelWorkflow() ,它确实取消了工作流程,但不是立即取消。 发生的情况是取消运行的代码,但随后我的工作流程继续创建下一个任务,然后在遇到下一个任务 onTaskChanged 活动时进入睡眠状态。 只有在进入睡眠状态后,工作流才会终止,而不是在调用 CancelWorkflow 时终止。
这导致了一个明显的问题,我不想创建下一个任务。 我调用 CancelWorkflow 是因为我希望它立即取消。
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.
发布评论
评论(3)
此 MSDN 主题中有很多建议:
以编程方式终止 SharePoint 工作流
这是一篇博客文章,简洁地包含完全相同的信息:
取消 SharePoint 工作流
最后,最具体的是,您需要使用静态方法:
SPWorkflowManager.CancelWorkflow(SPWorkflowworkflowInstanceToBeCancelled)
编辑
CancelWorkflow 是一个静态类,因此我修改了调用。
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'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.
这是一个相当老的问题,但是使用这段代码可以很容易地立即取消工作流:
本例中的工作流属性是
CancelWorkflow 方法会将工作流标记为“取消”,但直到暂停才会停止它(例如等待 ontaskchanged 事件) )或工作流程中的异常。 因此, throw new Exception() 的下一个语句将停止工作流程并将其状态设置为“已取消”。
This is pretty old question, but to cancel workflow from itself right away is easy using this piece of code:
workflowProperties in this example is
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".