Pergunta

I have created a list item event receiver(ItemUpdated).

When a item is updated in a list(for which event handler is attached),my code updates other items (may be more than 1) in lists in subsites.

When I debug the event receiver,I see that the ItemUpdated is called many times even if I update a single item in a list to which event handler is attached.

The code is working fine but I am not sure why ItemUpdated is called multiple times,even if a single item is updated.

Any idea?

Update : I install the eventhandler using xml below:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="Lists/NewIdeas">
    <Receiver>
      <Assembly>.....</Assembly>
      <Class>......</Class>
      <Type>ItemUpdated</Type>
      <Name>....</Name>
      <Synchronization>Synchronous</Synchronization>
      <SequenceNumber>1000</SequenceNumber>
    </Receiver>
  </Receivers>
</Elements>
Foi útil?

Solução

I think the problem is in the way you installed the event receiver.

"ListUrl" works only if feature has Scope=”Web”. In case the feature has Scope=”Site”, event receiver is fired for every list, ListUrl is ignored. Check this for more details : http://extreme-sharepoint.com/2011/12/27/event-receivers-sharepoint-2010/

So, in your case the event receiver is attached to every list and hence the problem.

The solution is to change the scope from "Site" to "Web". Remember to disable and uninstall the current feature first.

Outras dicas

If I understand correctly your event receiver updates item in the list that has the same event receiver. Thus your receiver updates item, item is updated, receiver is called, receiver update item, item is updated.... and so on... To solve this issue you can disable event firing in your receiver by this code:

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

internal class SPItemEventReceiverHandling : SPItemEventReceiver
{
    public new void DisableEventFiring()
    {
        base.DisableEventFiring();
    }

    public new void EnableEventFiring()
    {
        base.EnableEventFiring();
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top