SharePoint 工作流:如何在不再次触发工作流的情况下更新项目 [英] SharePoint Workflow: how to update the item without triggering the workflow again
问题描述
我有一个 SharePoint 工作流程,它在项目更改时运行.工作流与外部 REST 服务进行通信.如果服务返回一个字符串,我想用该字符串更新字段值之一.不幸的是,一旦当前工作流终止,此更新将触发此项目的另一个工作流实例.我最终陷入了无限循环!
I have a SharePoint workflow which is running whenever the item changes. The workflow communicates with an external REST service. If the service returns a string, I want to update one of the field values with that string. Unfortunately, this update will trigger another instance of the workflow for this item once the current workflow terminates. I end up with an infinite loop!
如何防止这种情况发生? SPListItem 具有 Update()、UpdateOverwriteVersion() 和 SystemUpdate() 方法,但它们似乎都无法阻止触发后续工作流.
How I can prevent this from happening? SPListItem has Update(), UpdateOverwriteVersion(), and SystemUpdate() methods but none of them seem to prevent subsequent workflows from being triggered.
如果上次更新发生在特定时间跨度内,我可以检查项目的上次修改时间戳并终止工作流程,但我正在寻找更强大的解决方案.
I could inspect the last modified timestamp of the item and terminate the workflow if the last update happened within a certain timespan, but I am looking for a more robust solution.
推荐答案
您可以使用一些扩展方法来静默更新项目.
You could use some extension method to update item silently.
public static class SPListItemExtensions
{
/// <summary>
/// Provides ability to update list item without firing event receiver.
/// </summary>
/// <param name="item"></param>
/// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
public static void Update(this SPListItem item, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.Update();
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.Update();
}
}
/// <summary>
/// Provides ability to update list item without firing event receiver.
/// </summary>
/// <param name="item"></param>
/// <param name="incrementListItemVersion"></param>
/// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
public static void SystemUpdate(this SPListItem item, bool incrementListItemVersion, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.SystemUpdate(incrementListItemVersion);
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.SystemUpdate(incrementListItemVersion);
}
}
/// <summary>
/// Provides ability to update list item without firing event receiver.
/// </summary>
/// <param name="item"></param>
/// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
public static void SystemUpdate(this SPListItem item, bool doNotFireEvents)
{
SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
if (doNotFireEvents)
{
try
{
rh.DisableEventFiring();
item.SystemUpdate();
}
finally
{
rh.EnableEventFiring();
}
}
else
{
item.SystemUpdate();
}
}
private class SPItemEventReceiverHandling : SPItemEventReceiver
{
public SPItemEventReceiverHandling() { }
new public void DisableEventFiring()
{
base.DisableEventFiring();
}
new public void EnableEventFiring()
{
base.EnableEventFiring();
}
}
}
这篇关于SharePoint 工作流:如何在不再次触发工作流的情况下更新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!