Condividi tramite


Error "operation is not valid due to the current state of the object" with custom event handler

Recently I had a customer who was hitting “Operation is not valid due to the current state of the object” error randomly on the document library where he had enabled his custom event handler (ItemUpdated). Event handler itself was pretty simple with just few lines of code to update the List Item’s name to a calculated one.

 

Interestingly, he was able to upload the XML, Doc and PDF files properly without any issues but it was failing only when he was trying to upload Adobe Portfolio or Zip files. This was interesting as Adobe Portfolio files are again PDF files only! Why would SharePoint not like Portfolio or Zip files?

 

Reading further about Adobe Portfolio files, noticed that almost all these files are large in size ( > 10-15 MB each), this lead to the thought that it has to do with the file size of the file being uploaded and not the type. I created  a clean machine with SharePoint 2010 and installed the event handler for my document library. From the customer’s stack trace, I had seen that it was crashing at CheckIn method call. So enabled Check-in/Check-out on the document library.

 

Now when I tried uploading small documents (1-5 MB each), it worked perfectly as it should and the event handler executed and updated the item name. Then I increased the file size of my DOCX file and made it 19 MB, now I am getting the crash consistently. So with all the file types with size of more than 20 MB or so caused the issue.

 

Now the troubleshooting, as the customer was using ItemUpdated event handler and then again calling SPListItem.Update() method within this event handler, it was causing the clash when SPFile.CheckIn was being called by the user from SharePoint UI. Now as the ItemUpdated event handler is asynchronous and the clash is happening.

 

Now SharePoint 2010 gives us control over the post event handlers, if they should be asynchronous or synchronous. By default all post-event handlers (ending with –ed) are asynchronous. So went ahead and added the following line in ELEMENTS.XML file:

<Synchronization> Synchronous </Synchronization>

Retracted the solution and deployed back again. Now the test time. Uploaded a 30 MB Word document and it went through without any errors. Tested again with different file sizes and different file types and it is working smoothly now. Just to make sure that it is the event synchronization that is working here, I removed the Synchronization element from the ELEMENTS.XML and redeployed the solution and I am back with the error.

 

So effectively, Synchronization element saved the day in the end.

 

This https://msdn.microsoft.com/en-us/library/ff512765.aspx gives the Synchronization element details.

The article https://msdn.microsoft.com/en-us/library/ee536171.aspx gives the details of event handler improvements in SharePoint 2010.

 

Hope this helps in saving some of your time Smile

 

-Manpreet