How to stop the ItemUpdated-event from refiring itself in an remote event receiver

tl;dr: If you have trouble with stopping the updated-event from firing, compare what you want to change in the beforeProperties and afterProperties. If they are different, you should go ahead with your update, otherwise not.

This was my scenario: I had created a remote event receiver that was attached to a document library and was supposed to act whenever an item in the list was updated. The initialization of the event receiver looks like this:

And all was fine in the world. The event receiver was nicely attached and got busy when I uploaded a document. But then it started acting out and making trouble. I was trying to change permissions on an item and this was the part of my code that was creating the problems:

Inside my SPRemoteEventType.ItemUpdated I did an Update and making it trigger itself. Recursive SharePoint with no end in site.. If you’re dealing with a event receiver with access to server side code, this is not a problem.

Writing that makes your world pink and sparkly again. But this is the barren end empty world of the remote event receiver and no such this is available.

How I solved it

Googleing this helped me not one bit and my first solution was something I would like to hide at the bottom of some server (I don’t want to talk about it, let’s just say it had something to do with counting the seconds since the last update).

At your disposal on SPRemoteEventProperties you have afterProperties and beforeProperties, found by doing this:

And those were the key to the problem: how to act on the firing only when the user changes something and not when it updates itself? By comparing the values in the beforeProperties with the afterProperties I could see if they contained a difference. If it doesn’t, the updating event should not happen again.

In my case I was changing the value of a custom property named CMIsSecret, so if that property was the same in beforeProperties as in afterProperties (the values before and after the save) I should go on with the update, otherwise not. This was the method I created:

It checks if the property exists at all (it doesn’t if it is an ItemAdded-event and the property is custom made) and if it differs. It returns true if the property should be updated and the final usage looked like this:

Where ”ChangePermissionSettings” changes and updates the item.

Hope it helps!

2 reaktion på “How to stop the ItemUpdated-event from refiring itself in an remote event receiver

  1. Thomas Gielissen

    Hi, i’m dealing with the same issue, and i’ve also make a ”CheckIfItemNeedToBeUpdated” method.
    The issue i’m dealing with is that, from within my item updated event receiver, i’m updating the item again (as you do). But i’m also writing some values in specific item fields. For example in the update method i do
    item[”documentType”] = ”Advertising”
    item.Update();

    After the item.Update() the event receiver is immediately triggered again, however when I load the item and check item[”documentType”], it’s back to it’s original (and empty) value.

    do you or anyone else has the same issue?

    Svara
  2. Stina Qvarnström

    Are you checking the afterProperties and beforeProperties? Also, try and use ParseAndSetFieldValue
    msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.listitem.parseandsetfieldvalue%28v=office.14%29.aspx

    Svara

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *