Itemupdating e

Add Or Update Item(String bstr Url, String bstr List Name, Boolean b Add, Boolean b System Update, Boolean b Preserve Item Version, Boolean b Update No Version, Int32& pl ID, String& pbstr Guid, Guid pbstr New Doc Id, Boolean b Has New Doc Id, String bstr Version, Object& pvar Attachment Names, Object& pvar Attachment Contents, Object& pvar Properties, Boolean b Check Out, Boolean b Checkin, Boolean b Migration, Boolean b Publish String bstr Url, String bstr List Name, Boolean b Add, Boolean b System Update, Boolean b Preserve Item Version, Boolean b Update No Version, Int32& pl ID, String& pbstr Guid, Guid pbstr New Doc Id, Boolean b Has New Doc Id, String bstr Version, Object& pvar Attachment Names, Object& pvar Attachment Contents, Object& pvar Properties, Boolean b Check Out, Boolean b Checkin, Boolean b Migration, Boolean b Publish String bstr Url, String bstr List Name, Boolean b Add, Boolean b System Update, Boolean b Preserve Item Version, Boolean b Update No Version, Int32& pl ID, String& pbstr Guid, Guid pbstr New Doc Id, Boolean b Has New Doc Id, String bstr Version, Object& pvar Attachment Names, Object& pvar Attachment Contents, Object& pvar Properties, Boolean b Check Out, Boolean b Checkin, Boolean b Migration, Boolean b Publish Boolean b Add, Boolean b System, Boolean b Preserve Item Version, Boolean b No Version, Boolean b Migration, Boolean b Publish, Boolean b Check Out, Boolean b Checkin, Guid new Guid On Add, Int32& ul ID, Object& obj Attachment Names, Object& obj Attachment Contents, Boolean suppress After Events) 3215 I spent some time writing test code to try and reproduce the problem when multiple threads are doing updates to the same file and I finally managed to get the error.

There is no exception thrown if we first update item2 and then item1.

All of them call the internal method Update Internal() which has some interesting arguments like The experiment showed that all of them but one were leading to an exception being thrown. So one way to fix the problem is changing your event receiver to update the list item using System Update(false).

Some people are additionally recommending to disable the event firing when calling the update as well: This will work well unless you actually want to create a new version of the list item or call any of the other update methods for whateever reason.

All this explains why the issue is not happening all the time but only when specific race conditions are met.

The next question is of course how do we prevent the problem from happening.

This error occurs when you upload a file to a document library through the Share Point UI and when the document library has an event receiver which updates the list item on Item Added(). on ….” and the callstack leads to a COMException in the Microsoft. It is running in a single thread but it represents exactly what is happening with the event receiver and the Edit pages if you think of it this way: All objects with index “1” are accessed from the event receiver and all objects with index “2”are accessed from the Edit code-behind.

The error doesn’t happen all the time which lead us to conclude that it is due to some race conditions, especially knowing that event receivers are run in separate thread. The issue only happens when the code is executed in the exact sequence listed above.

Visual Studio will then add a new WCF service to your web application project named App Event

This helps big time and solves 90% of the causes of this error I’ve seen.

If needed you can additionally add more advanced thread synchronisation techniques to your code such as lock statements, mutexes, semaphors etc.

If you don’t agree with me about the “goto” statement then look at this different but similar problem caused by race conditions and how it is solved using a try-catch-goto block in this Microsoft article: if you are getting the error in your own code it is due to an SPSite created in a completely different branch of the solution and then SPWebs or SPLists are being passed as arguments down to your function where you try to update the list item.

To resolve this error always remember to create a new fresh SPSite object to retrieve the list and the file objects from, in the same method where you are doing the update.