I have been working with a client to create a unique document numbering system on their MOSS 2007 intranet and have came across a really frustrating issue. The idea was simple enough an event receiver associated with all document libraries which sets a field to have a unique number. In practice this provided extremely frustrating.
I created the event receiver and was using the ItemAdded method to get the number from a list at the root of the site collection then setting this number to a column on the list. What I found was certain times this was working and others it wasn’t. I started outputting information to a log file to track what was happening and I could see everything was working as expected and even after updating the item in SharePoint the reference number was set, however when I looked at the UI there were times when it was empty.
Eventually when reviewing the ULS log I noticed a pattern when the reference number was not getting set. On all items regardless if they worked on not there was always details displayed in the log about getting document Metadata, see below. I noticed on the items where the reference number was set to what I expected this call happened before I set the reference number whereas on items where it failed the call was after.
Details in ULS Log
RPC method = getDocsMetaInfo
in getDocsMetaInfo after getlinks
I had a quick look on Google and found out this is SharePoint getting the Metadata properties on the document. All of a sudden I realised this made sense as when the call was after I set the reference number it must have been overwriting my details with those from the document.
I tried to figure out a way to get around this but in the end there was no way I could see that would always ensure this function was called after so I put in a 3 second delay before assigning the reference number and it started to work on a regular basis.
Obviously this isn’t ideal as there is still a chance the getDocsMetaInfo function could be called after so if anyone else has a better idea let me know.