Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint Workflow: how to update the item without triggering the workflow again

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!

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.

like image 819
Philipp Schmid Avatar asked Mar 17 '10 23:03

Philipp Schmid


People also ask

What prevents a flow from triggering itself?

The best and most efficient way is to add a trigger condition inside the trigger settings. The trigger condition blocks the flow from starting if the condition is not 'true'. You can not only avoid the infinite trigger loop with all the updates, but also save flow runs. No more empty flow runs.

When an item is created or modified SharePoint flow?

When you create a flow with SharePoint trigger like "When an item is created or modified", you want your flow only triggers when the new item is created or modified. However, when you turn off the flow, make modification to the items, then turn on the flow, the flow still triggers for that item's modifications.

Are SharePoint Workflows going away?

SharePoint 2010 workflows are deprecated but will remain supported for the SharePoint Server Subscription Edition release until July 14, 2026.


1 Answers

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();
    }


   }
}
like image 56
Janis Veinbergs Avatar answered Oct 16 '22 23:10

Janis Veinbergs