Search This Blog

Monday, December 27, 2010

How to copy attachments from one list item to another

private void CopyAttachments(SPListItem sourceItem, SPListItem targetItem)
{
try
{
//get the folder with the attachments for the source item
SPFolder sourceItemAttachmentsFolder =
sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
//Loop over the attachments, and add them to the target item
foreach (SPFile file in sourceItemAttachmentsFolder.Files)
{
byte[] binFile = file.OpenBinary();
targetItem.Attachments.AddNow(file.Name, binFile);
}
}
catch { }
finally
{
sourceItem.Web.Dispose();
}
}

No comments:

Post a Comment