Disable ItemUpdated Endless loop in Event Receiver

In one of my project, I had a requirement to build an event receiver which is for item updated.

The logic is when I update the document, programmatically my custom field will be updated with a value.

So I thought of adding event receiver for that and wrote below code:

public override void ItemUpdated(SPItemEventProperties properties)
{
    base.ItemUpdated(properties);
    SPListItem listItem = properties.ListItem;
    listItem["MyCustomField"] = "Okay, I’m updated!";
    listItem.Update();
}

When I ran this code with debugging,

listItem.Update();

fired and event receiver called again as item is being updated so my break-point started over from

base.ItemUpdated(properties);

It means again ItemUpdated event receiver will be called.

So this was like endless loop. There must be some workaround for this which is as follow: we need to write updating code in a way that it doesn’t trigger event receiver again.

So I have created below DisabledEventsScope class in the event receiver file.

class  DisabledEventsScope : SPItemEventReceiver, IDisposable
{
    // Boolean to hold the original value of the EventFiringEnabled property
    bool _originalValue;
    public DisabledEventsScope()
    {
        // Save off the original value of EventFiringEnabled
        _originalValue = base.EventFiringEnabled;

        // Set EventFiringEnabled to false to disable it
        base.EventFiringEnabled = false ;
    }
    public void Dispose()
    {
        // Set EventFiringEnabled back to its original value
        base.EventFiringEnabled = _originalValue;
    }
}

This class will turn off the firing of the event receiver by setting Boolean value of event receiver, run the code without endless loop. We need to implement this class in our code so that it disable event firing.

public override void ItemUpdated(SPItemEventProperties  properties)
{
    base.ItemUpdated(properties);
    using (DisabledEventsScope scope = new DisabledEventsScope ())
    {
        SPListItem listItem = properties.ListItem;
        listItem["MyCustomField"] = "Okay, I’m updated!";
        listItem.Update();
    }
}

Once this code is running, it won’t be firing endless loop of event receiver.

Happy SharePointing.!!

SharePoint Debugging: Attach Correct W3WP Process

Being a developer, debugging is necessity. To debug we many times, face that there are more than one W3WP processes in Process list. I was confused that which is the exact process to which I should be debugging.
This process doesn’t contain information about W3WP process like Application Pool details.

W3WP-list

To know more about that process, we need to follow below steps:

  1. Open Command prompt by administrator.
  2. Navigate to folder “C:\Windows\System32\inetsrv” by running following command:
    cd “C:\Windows\System32\inetsrv”
  3. Execute this command:
    appcmd.exe list wp
    It will list down the worker process with process id and application pool name.W3Wp-pool-list

We already have our application pool detail for the site we are debugging. From the above list we should note down the process id and then in Visual studio, attach the process only to that process with specific ID we noted.

Now we are done with “Attach to Process”.
Happy debugging!