Checking File Status in an EventHandler

So a common issue people run in to with file driven event handlers in SharePoint is that the event handler triggers on the SPListItem being added not the file write completion. So oftentimes you’ll get odd issues where unpredictably you won’t be able to access the file within your event handler (typically a NullReferenceException). So here’s a simple example for how to see if the file upload has been completed and added successfully to the list item.

All this code does it try and read some of the SPFile properties off of the SPListItem. If it’s completed and returns the properties properly it returns true, otherwise it returns false. Optionally, I have a recursive loop to keep on checking until the file is found but you’d probably want to add a counter to that bit so it doesn’t go in to an infinite loop or have memory leaks.


  1. internal bool CheckFileStatus(SPListItemEventProperties properties)
  2. {
  3. SPFile file = properties.ListItem.File;
  4. bool finished = false;
  5. try
  6. {
  7. if(file.Title != null && file.Title != “”)
  8. {
  9. finished = true;
  10. }
  11. }
  12. catch(Exception ex)
  13. {
  14. Debug.WriteLine(“File has not finished being added to list”);
  15. Thread.Sleep(5000);
  16. CheckFileStatus(properties);
  17. }
  18. if(finished == true)
  19. {
  20. //do something with completed file 🙂
  21. }
  22. return finished;
  23. }
  1. No comments yet.
(will not be published)

%d bloggers like this: