Thursday, 20 November 2008

Why "ItemUpdated" fires 2 times ?

Problem:
I have a document library with an "ItemUpdated" item eventreceiver. Each time I upload a document, the eventreceiver fired 2 times in two different threads, with update conflicts as a concequens.

Cause:
When you use check-in/check-out on your document libraries the check-in action also fires an ItemUpdated event. If found this information here (good article!):

Solution:
We need to check which "ItemUpdated" we deal with ... the one from the check-in or the 'real' one. If found this code on the MS site to check the source of the event:

if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
//This is when the update event is triggered by
check-in.
}
else
{
//This is triggered by events other than check-in
action.
}

Source: http://support.microsoft.com/kb/939307/en-us

4 comments:

Anonymous said...

SPEventReceiverBase.DisableEventFiring didnot work ?

Anonymous said...

SPEventReceiverBase.DisableEventFiring won't work because it's not an event that's triggered from within the code - it's an inherent set of events that are triggered outside of our control by SharePoint.

Anonymous said...

thanks!

kesav7902 said...

but u can avoid this by following this code snippet approach....

properties.disableeventfiring()

properties.update();

properties.eventfiring();