If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Tuesday, July 3, 2012

SharePoint ItemAdded OpenBinary Zero

I created an ItemAdded event receiver for a document library. This sets metadata values for an uploaded file. Everything worked well for documents added using SharePoint's single, or multiple file upload forms.

However, when using Windows Explorer to upload a file to the library, I found that a call to the OpenBinary method for the file returned a zero length byte array.

In fact the wider issue was that the length of the file object was zero. Here's some skeleton code:

public override void ItemAdded(SPItemEventProperties properties)
  long length = properties.ListItem.File.Length; // zero

Other people that have found the same issue suggest using Thread.Sleep() in the event receiver to add an artifical delay.

However, I chose to work around this issue another way:

  1. In the ItemAdded receiver, do a check for a zero length file. Don't do any processing if it is zero. Obviously this won't work for a file uploaded using Windows Explorer. So I also needed to...
  2. Add an ItemUpdated event receiver (which just so happens to called when a user uploads a file using Windows Explorer.
  3. Both receivers call some shared code which updates the file's metadata. However, to avoid this being done multiple times, the shared code runs a check to verify that the metadata is blank before it is set.