2

I have fallen in a serious problem about the SPListItem Update method. When I make a event receiver to intercept SPListItem change or update with SPItemEventReceiver ItemUpdated method, the event is recursively called.

source:

  public override void ItemUpdated(SPItemEventProperties properties)
   {
    base.ItemUpdated(properties);

    SPList list = properties.List;

    SPListItem item = properties.ListItem;

    if ("Oui" == properties.ListItem["Envoyé vers le Directeur"].ToString())
    {
        SPListItem copyItem = item;

        MoveItemsToFolder(properties, list, "Dossiers envoyés Rédaction", copyItem);

        item["Service"] = "direction";
        item.Update();

        string[] cols = { "Envoyé vers le Directeur", "Archivé" };
        HideColumns(list, cols);

    }
    else if ("Oui" == properties.ListItem["Validé par le Directeur"].ToString())
    {

        SPListItem copyItem = item;

        MoveItemsToFolder(properties, list, "Direction", copyItem);

        item["Service"] = "bureauOrdre";
        item.Update();

        string[] cols = { "Validé par le Directeur", "Envoyé vers le Directeur" };
        HideColumns(list, cols);
    }
    else if ("Oui" == properties.ListItem["Archivé"].ToString())
    {

        SPListItem copyItem = item;

        MoveItemsToFolder(properties, list, "Archives", copyItem);

        item["Service"] = "Archive";
        item.Update();

        string[] cols = { "Validé par le Directeur", "Envoyé vers le Directeur" };
        HideColumns(list, cols);
    }
    else
    {

        item["Service"] = "redaction";
        item.Update();

    }

}

1 Answer 1

2

The solution is to use SPItemEventReceiver.EnableEventFiring MSDN

You can do

var eventRecTemp = new SPItemEventReceiver();
eventRecTemp.EnableEventFiring = false;
//update
eventRecTemp.EnableEventFiring = true;

or as this blog talks about, turn it into a scope using IDisposable.

I used the scope method in an ItemUpdated heavy project and it worked well.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.