Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

copying column items for one column to another within the SharePointlist

$
0
0

Hi, i have a SPList and want to copy existing column "x" items to new created "y"column. I can copy the whole items from one list to another, but unable to copy columns within the same list, here is the my console app(from one list to another). 

   static void Main(string[] args)
        {
           //copying list items from one list to another
          using (SPSite site = new SPSite(siteurl))
        {
        using (SPWeb web = site.OpenWeb())
        {
            CopyItems(web, "list1", "list2");
        }
    }
}

private static void CopyItems(SPWeb web, string sourcelistname, string destlistname)
{
    SPList sourceList = web.Lists[sourcelistname];
    SPList destList = web.Lists[destlistname];

    foreach (SPListItem item in sourceList.Items)
        CopyItem(item, destList);
}

private static void CopyItem(SPListItem item, SPList destList)
{
    SPListItem newItem = destList.Items.Add();

    for (int i = 0; i < item.Fields.Count; i++)
        if ((!newItem.Fields[i].ReadOnlyField) && (newItem.Fields[i].InternalName != "Attachments"))
            newItem[newItem.Fields[i].InternalName] = item[newItem.Fields[i].InternalName];

    newItem.Update();
}
       //end
}
}

how can i do same, copying one column to another column within the same list?




Viewing all articles
Browse latest Browse all 7589

Trending Articles



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