locked
How to copy SharePoint List items to another SharePoint List Using CSOM ? RRS feed

  • Question

  • Using console app is there any possibility of copying List items to another list along with version history
    Wednesday, March 1, 2017 11:40 AM

All replies

  • Please use below code to move list items from one list to another list in same site...However, to get version about the item,its not feasible in CSOM(.Net Managed Code).Since, the property called Items.versions available only in server object model.

    ClientContext context = new ClientContext("<site URL>");
    
    Web web = context.Web;
    List ListA = web.Lists.GetByTitle("ListA");
    
    ListItemCollection itemCollection = ListA.GetItems(CamlQuery.CreateAllItemsQuery());
    context.Load(itemCollection);
    context.ExecuteQuery();
    
    foreach(ListItem item in itemCollection) {
    
     string TitleValue = Convert.ToString(item["Title"]);
     ListItemCreationInformation createListItem = new ListItemCreationInformation();
     List ListB = web.Lists.GetByTitle("ListB");
     ListItem newItem = ListB.AddItem(createListItem);
     newItem["Title"] = TitleValue;
     newItem.Update();
     context.ExecuteQuery();
    
    }

     
    Wednesday, March 1, 2017 2:10 PM
  • Hi,

    In client side, we can get the list items with version history using web service(/_vti_bin/Lists.asmx) or File.Versions to achieve it, then add items into another list.

    The following articles for your reference:

    SharePoint 2013: Retrieve The List Item Version History And Attachment URLs Using SharePoint Web Services

    http://www.c-sharpcorner.com/UploadFile/sagarp/sharepoint-2013-retrieve-the-list-item-version-history-and/

    How to get list item version history of a custom list using Client Object Model

    http://blog.softreeconsulting.com/sharepoint/how-to-get-list-item-version-history-of-a-custom-list-using-client-object-model/

    How to: Create, Update, and Delete List Items

    https://msdn.microsoft.com/en-us/library/office/ee539976(v=office.14).aspx

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com



    • Edited by Dennis Guo Thursday, March 2, 2017 2:20 AM
    • Proposed as answer by Patrick_Liang Friday, March 10, 2017 8:41 AM
    Thursday, March 2, 2017 2:17 AM