Monday, 7 May 2012
SharePoint 2010, event receivers and Save Conflict error.
Recently I tried to modify all images right after they were uploaded to the library.
I went for ItemAdded event receiver to be sure all metadata etc is there. Then I hit this error:
Save Conflict. Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.<nativehr>0x81020037</nativehr><nativestack></nativestack>
Of course ItemAdded event is asynchronous so there is a big chance SharePoint, workflow or other event receiver was still changing something and I was caught up in the middle of this or my changes executed other event receivers like ItemUpdating.
So to fix it there are t hings that need to be done.
1) Make things synchronous.
In my case asynchronous event has to be made synchronous and fortunately SharePoint 2010 introduce Synchronization (both in CAML and code)
So simple
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers >
<Receiver>
[...]
<Synchronization>Synchronous</Synchronization>
[...]
</Receiver>
</Receivers>
</Elements>
or
[...]
eventReceiver.Synchronization =
SPEventReceiverSynchronization.Synchronous;
[...]
will solve it.
2) Disable event firing
To avoid other event receivers to fire we need to change EventFiringEnabled flag. You can do it in various different ways but I particularly like this approach: http://buyevich.blogspot.co.uk/2010/10/disableeventfiring-is-obsolete-in.html
This way you won't fire other events (unless necessary) and won't end up in the loop when using ItemUpdating or ItemUpdated.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment