我有一个 SharePoint 工作流 它是 每当项目更改 时运行。工作流与外部 REST 服务进行通信。如果服务返回一个字符串,我想 用该字符串更新字段值 之一。不幸的是,一旦当前工作流终止,此更新将触发此项目的另一个工作流实例。 我以无限循环告终!

如何防止这种情况发生? SPListItem 有 Update()、UpdateOverwriteVersion() 和 SystemUpdate() 方法,但它们似乎都没有阻止后续工作流被触发。

如果上次更新发生在某个时间跨度内,我可以检查项目的上次修改时间戳并终止工作流程,但我正在寻找更强大的解决方案。

最佳答案

您可以使用一些扩展方法来静默更新项目。

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 工作流 : how to update the item without triggering the workflow again,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2466533/

10-13 08:58