SharePoint - Picture Library - How to copy picture from one Picture Library to another

Hi All,

Here it is interesting blog post of an eventhandler.Below is the requirement for the task.

There are two picture libraries available, i need to copy picture from one library to another when picture is going to upload in picture library.For that i've created "ItemAdded" event handler and do code on this event.

You can find the code below:

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
try
{
SPListItem item = properties.ListItem;
byte[] picFile = null;
SPFolder Sourcelibrary = item.Web.Folders[name of the source picture library];
picFile = item.File.OpenBinary();
if (picFile != null && picFile.Length > 0)
{
SPFolder DestLibrary = item.Web.Folders[name of the destination picture library];
item.Web.AllowUnsafeUpdates = true;
DestLibrary.Files.Add(System.IO.Path.GetFileName(item.File.Name), picFile);
}
}
catch (Exception)
{
throw;
}
}

No comments: