Quantcast
Channel: SharePoint Legacy Versions - Using SharePoint Designer, InfoPath and Other Customizations Forum
Viewing all articles
Browse latest Browse all 2319

Copy ListItems between Identical Lists in same SharePoint site with a ContentType available in it

$
0
0

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:

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;
        }

But, I can't see items being copied. It is throwing exception "Use SPFileCollection.Add to add items". Please help.

Thanks,


Viewing all articles
Browse latest Browse all 2319

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>