3

I have a custom list (not document library) that i have attached 2 event receivers to. These are itemupdating and itemupdated. My itemupdating event receiver sets a value in a field and my itemupdated sets some permissions on the item. I've noticed that the after the itemupdated method is called the itemupdating method is called again. I presume this is because the i've called properties.ListItem.Update(); I do not want the itemupdating event code to run twice. How can i handle this scenerio?

public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
            if (properties.ListTitle != C.EquipmentList) return;

            //Expiry date calculated field is not available in this event so
            //im recreating the formula to pass to the checkexpirydate method
            DateTime ExpiryDate = CalculateExpiryDate(
                properties.AfterProperties[C.InternalLastCalDate].ToString(),
                properties.AfterProperties[C.InternalNumberOfDaysTillNextCalibration].ToString()
                );

            properties.AfterProperties[C.Status] = CheckExpiryDate(ExpiryDate);
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            try
            {
                base.ItemUpdated(properties);
                if (properties.ListTitle != C.EquipmentList) return;

                properties.Web.AllowUnsafeUpdates = true;
                properties.Web.Site.AllowUnsafeUpdates = true;

                RemoveItemPermission(properties);
                SetCustodianPermissions(properties);

                SPGroup AdminSGroup = properties.Web.SiteGroups[C.AdminsGroup];
                GrantPermission(properties.ListItem, properties.Web, SPRoleType.Administrator, AdminSGroup);

                EventFiringEnabled = false;
                properties.ListItem.SystemUpdate();

            }
            finally
            {
                EventFiringEnabled = true;
                properties.Web.AllowUnsafeUpdates = false;
                properties.Web.Site.AllowUnsafeUpdates = false;
            }
        }

1 Answer 1

1

The issue is that these are in two different event receivers. EventFiringEnabled is a thread specific property. The reason it's done this way is that this could cause code from 3rd parties or other developers to function unexpectedly. If you put those into a single event receiver, I would expect that it would work the way you want it to.

I'm looking for an MSDN reference for this.

Edit:
I cannot seem to find anything about this specifically in any MS documentation. But here is the article where I read about this when I was experiencing the same issue. Notice that the article was from the 2007 days.

Disable event firing in SharePoint when updating a list item outside of an event handler

Upon disassembling Microsoft.Sharepoint.dll, I discovered that the above mentioned method actually sets a static, thread-specific, property of SPEventManager class called EventFiringDisabled

1
  • 1
    Thanks for the answer. Actually i think i found the problem. The SetCustodianPermissions was also calling update on the item without disabling the event firing. I removed the update from the SetCustodianPermissions method and it started working as expected. Commented Mar 26, 2014 at 11:06

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.