none
List item versions using CSOM (for the umpty-umpth time) RRS feed

  • Question

  • Hi everyone.

    Im currently building a console application for moving items from one list to another and I want to keep the version history.

    Since the hosting IT department does not permit deploying server side solutions this has to be accomplished using CSOM or existing services.

    Is this possible???

    The two approaches I have managed to find so far are either using HTML scraping of the version dialog page or using Lists.asmx web service and neither seems to be reliable.

    HTML scraping seems does not expose all fields, formatting has been applied and it uses display names.

    Using GetVersionCollection also seems like a dead end. It has been deprecated but mainly its because if a field value for a specific list item version is empty, that version wont be present in the collection at all. This means we cannot link the version in the field to an actual list item version using the position (i.e first = most recent version, last = initial version).

    e.g.

    1. Create an item setting field "X" to "foo" and leave field "Y" empty.
    2. Update the item setting X to empty and Y to "bar".
    3. Getting the version collection for either of those fields will return a collection consisting of just one version,
      "foo" for "X" and "bar" for "Y" and no way to know what list item version those field values correlate to.

    The precision of the modification time is limited to seconds, which makes matching fail if updates takes place during the same second.

    This is really getting on my nerves and if someone has a solution to share I would really appreciate it.

    Regards

    //Joakim

    Tuesday, January 10, 2017 10:13 PM

All replies

  • Hi

    using webservice to retrieve the item versions

    http://sharepoint.stackexchange.com/questions/128527/get-list-item-version-history-using-client-side-object-model-c


    顺其自然地勇往直前!—Justin Liu

    Wednesday, January 11, 2017 12:45 AM
  • Hi Justin,

    I tried using the service but as I wrote in the initial post, it is buggy when it comes to fields that have been empty at one or more occations.

    //Joakim


    Wednesday, January 11, 2017 6:00 AM
  • Hi Joakim,

    We can get the file version collection using SharePoint CSOM like below:

    using (ClientContext ctx = new ClientContext(“SITE_URL”))
    
    {
    
    CamlQuery camlQuery = new CamlQuery();
    
    Web web = ctx.Web;
    
    ctx.Load(web,
    
                        w => w.ServerRelativeUrl,
    
                        w => w.Lists);
    
    List list = web.Lists.GetByTitle(“LIST_TITLE”);
    
    ctx.Load(list);
    
    ListItemCollection itemColl = list.GetItems(camlQuery);
    
    ctx.Load(itemColl);
    
    ctx.ExecuteQuery();
    
    foreach (ListItem item in itemColl)
    
    {
    
    File fileVersion = web.GetFileByServerRelativeUrl(web.ServerRelativeUrl +    “/Lists/” + list.Title + “/” + item.Id + “_.000”);
    
    ctx.Load(fileVersion);
    
    FileVersionCollection fileVersionCollection = fileVersion.Versions;
    
    ctx.Load(fileVersionCollection);
    
    ctx.ExecuteQuery();
    
    for (int iVersionCount = 0; iVersionCount < fileVersionCollection.Count; iVersionCount++)
    
    {
    
    FileVersion version = fileVersionCollection[iVersionCount];
    
    fileVersionCollection.DeleteByID(version.ID);
    
    }
    
    ctx.ExecuteQuery();
    
    }
    
    }

    More information:

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

    Thanks

    Best Regards


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

    Wednesday, January 11, 2017 9:15 AM
  • Hi Jerry, 

    Unfortunately, this will only give me the version history of a list item.

    I'm looking for a way to extract each versions of the list item including the field values.

    Regards,

    //Joakim

    Wednesday, January 11, 2017 4:16 PM
  • I think there may be some incorrect code in your request, check it, it should work.

    顺其自然地勇往直前!—Justin Liu

    Thursday, January 12, 2017 12:29 AM
  • Just making sure I make myself 100% crystal clear:

    I do need: the list item data related to a specific version of a list item (e.g. Title, Description etc.) or a way to recreate it in some way.

    I don't need: the metadata related to a specific version (e.g. Version Label)

    As far as I understand, a file version object contains only the metadata about a certain version of a list item and not the actual version of the list item itself.

    Regards

    //Joakim

    Friday, January 13, 2017 9:18 AM
  • yes, you are rigth.

    Do you mean you want to get the chaned data for every version?

    I suggest you get that from version history page.


    顺其自然地勇往直前!—Justin Liu

    Friday, January 20, 2017 12:38 AM