Saving files in ItemUpdated for a SharePoint List

I had an issue today trying to call File.SaveBinary() method of an SPListItem in the ItemUpdated method. Everytime I treid to call it it seemed to throw an error. In the end there was an easy fix for this – make sure AllowUnsafeUpdates is set to true. Once this is set, the binary can be saved no problem. The other method you should call before you save the binary is the this.DisableEventFiring(); method to ensure that the event does not go into an endless loop.
 
ie 
this.DisableEventFiring();
SPListItem item = properties.ListItem;
item.Web.AllowUnsafeUpdates = true;
item.File.SaveBinary(updatedFile);
item.Web.AllowUnsafeUpdates = false;
this.EnableEventFiring();

Leave a comment