SharePoint 工作流 : how to update the item without triggering the workflow again

标签 sharepoint workflow

我有一个 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/

相关文章:

excel - 如何使用VBA从SharePoint下载文件?

javascript - 获取 Sharepoint 中文档的文档类型图标

sharepoint - Caml 查询 - 排序依据 - SharePoint 2013-CSOM

c# - 使用 Workflow 的 ASP.net 页面导航

git:更新当前分支

sharepoint - 用于 SharePoint 2010 计时器作业的帐户

JavaScript/SharePoint - 强制文件下载

delphi - Delphi 有 "Workflow Engine"实现吗?

javascript - SharePoint 列表项 : Copy list-item-level permissions from one item to another

javascript - 为 Javascript 构建自动化和部署