Modifying Image in Picture library on ItemAdded
- When a user adds an image to a picture library, I want to modify the picture and save it. However, I can't do this without getting an exception and consequent failure.
Here's what happens:
User chooses image to upload.
My event handler runs where I read the file from the ListItem object with OpenBinaryStream, modify the file with system.drawing, then save to the ListItem object with SaveBinary. I then do an item.Update().
User is presented with default SharePoint form to modify properties of image.
User clicks OK.
Exception gets thrown. (This item has been modified by ..... a la: http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/ )
I've searched high and low, but I just can't figure out what I am doing wrong or how to get rid of the error.
Answers
- Just a thought... try item.SystemUpdate()
Also, you may be triggering your own event reciever again when you update the file, in which case call DisableEventFiring() and EnableEventFiring() around your update code.
Also, if there is a timing problem, here is a related article: http://www.moss2007.be/blogs/vandest/archive/2007/12/27/sharepoint-2007-event-handling-a-renamed-splistitem.aspx
Mike Smith TechTrainingNotes.blogspot.com- Marked As Answer byAaron Han - MSFTModeratorFriday, November 06, 2009 10:26 AM
Hi,
Would you please follow Mike’s advice, using DisableEventFiring() and EnableEventFiring() and SystemUpdate(), and please also use item.File.Update.
Please try this event code snippet
--------------------------------------------------
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
SPFile file = item.File;
base.DisableEventFiring();
Stream stream = item.File.OpenBinaryStream();
Image image = Image.FromStream(stream);
//Here modify properties of your image
image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
item.File.Update();
item.File.SaveBinary(stream);
item.SystemUpdate(false);
base.EnableEventFiring();
}
--------------------------------------------------
Hope this can be helpful.
Best Regards,
-Aaron
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 06, 2009 10:26 AM
All Replies
- Just a thought... try item.SystemUpdate()
Also, you may be triggering your own event reciever again when you update the file, in which case call DisableEventFiring() and EnableEventFiring() around your update code.
Also, if there is a timing problem, here is a related article: http://www.moss2007.be/blogs/vandest/archive/2007/12/27/sharepoint-2007-event-handling-a-renamed-splistitem.aspx
Mike Smith TechTrainingNotes.blogspot.com- Marked As Answer byAaron Han - MSFTModeratorFriday, November 06, 2009 10:26 AM
Hi,
Would you please follow Mike’s advice, using DisableEventFiring() and EnableEventFiring() and SystemUpdate(), and please also use item.File.Update.
Please try this event code snippet
--------------------------------------------------
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem item = properties.ListItem;
SPFile file = item.File;
base.DisableEventFiring();
Stream stream = item.File.OpenBinaryStream();
Image image = Image.FromStream(stream);
//Here modify properties of your image
image.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
item.File.Update();
item.File.SaveBinary(stream);
item.SystemUpdate(false);
base.EnableEventFiring();
}
--------------------------------------------------
Hope this can be helpful.
Best Regards,
-Aaron
- Marked As Answer byAaron Han - MSFTModeratorFriday, November 06, 2009 10:26 AM

