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:
- 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...
- Add an ItemUpdated event receiver (which just so happens to called when a user uploads a file using Windows Explorer.
- 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.