Hi,
I have a custom List "Product Images" and want to move some of the records in this List to another identical list.
"Product Images" has contenttype "Picture" available in it.
I am using following code snippet available on web:
But, I can't see items being copied. It is throwing exception "Use SPFileCollection.Add to add items". Please help.public static void CopyItem(SPListItem sourceItem, SPFile file, string destinationListName) { //copy sourceItem to destinationList SPList destinationList = sourceItem.Web.Lists[destinationListName]; try { SPListItem targetItem = destinationList.Items.Add(); foreach (SPField f in sourceItem.Fields) { //!f.ReadOnlyField && if (!f.ReadOnlyField && f.InternalName != "Attachments") { targetItem[f.InternalName] = sourceItem[f.InternalName]; } } ////copy attachments /* foreach (string fileName in sourceItem.Attachments) { SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName); byte[] imageData = file.OpenBinary(); targetItem.Attachments.Add(fileName, imageData); }*/ targetItem.Update(); destinationList.Update(); }
catch (Exception ex)
{
string msg = ex.Message;
}
// return targetItem;
}
Thanks,