none
How to fetch List column values from Version history using JSOM or REST RRS feed

  • Question

  • I have a List with versioning enabled. I want to display the value of Title field for each version.

    For instance, for Version 1 the value of Title field is "First", for version 2 the value of Title field was changed to "Second", and for Version 3 the value of Title field was changed to "Third". In Version History page, I can see all these values with their corresponding Version Number. How can I fetch these data using JavaScript Object Model or REST API?

    I checked this thread, but the example shows fetching the URL, but I could not find a way to fetch the specific field value. I'm specifically looking for JSOM or REST approach. Any help is really appreciated. Thanks!

    Update: Though I have mentioned Title as an example, basically I'm looking at fetching version history of multiple columns (of course if the values have changed).
    • Edited by Suresh C Thursday, March 16, 2017 7:26 AM Added more clarity
    Wednesday, March 15, 2017 8:30 AM

Answers

  • Hi Suresh,

    You can view the version history using '/_layouts/15/versions.aspx'. Below code block may help you. I have tried to put my comments for your understanding. Please try and let me know if it works.

    Note : Please make sure to add required jQuery file references while testing the code.

    function getItemVersions(url, listId, itemId, success) {
        var versionsUrl = url + '/_layouts/15/versions.aspx?list=' + listId + '&ID=' + itemId;
        $.get(versionsUrl, function(data) {
            var versionEntries = parseVersionList(data);
        });
    }
    
    
    function parseVersionList(data) {
        var entries = {};
        var html = $.parseHTML(data);
        var versionList = $(html).find('table.ms-settingsframe');
    
        versionList.find('tbody > tr').each(function(i) {
            if (i > 0 && (i - 1) % 2 == 0) {
                var verRow = $(this); //get version row
                var propsRow = verRow.next(); //get properties row
                var versionLabel = verRow.find('td:first').html().trim();
    
                //get item properties from propsRow goes here
            }
    
        });
        return entries;
    }
    
    
    //Usage
    var webUrl = "https://xyz.sharepoint.com"; // Need to be changed
    var listId = "{2697fad3-57d8-455c-8a49-59a065b08b31}"; // Need to be changed
    var listItemId = 3; // Need to be changed
    getItemVersions(webUrl, listId, listItemId, function(versionEntries) {
        console.log(versionEntries);
    });
    

    Regards.

    Wednesday, March 15, 2017 9:44 AM

All replies

  • Hi Suresh,

    You can view the version history using '/_layouts/15/versions.aspx'. Below code block may help you. I have tried to put my comments for your understanding. Please try and let me know if it works.

    Note : Please make sure to add required jQuery file references while testing the code.

    function getItemVersions(url, listId, itemId, success) {
        var versionsUrl = url + '/_layouts/15/versions.aspx?list=' + listId + '&ID=' + itemId;
        $.get(versionsUrl, function(data) {
            var versionEntries = parseVersionList(data);
        });
    }
    
    
    function parseVersionList(data) {
        var entries = {};
        var html = $.parseHTML(data);
        var versionList = $(html).find('table.ms-settingsframe');
    
        versionList.find('tbody > tr').each(function(i) {
            if (i > 0 && (i - 1) % 2 == 0) {
                var verRow = $(this); //get version row
                var propsRow = verRow.next(); //get properties row
                var versionLabel = verRow.find('td:first').html().trim();
    
                //get item properties from propsRow goes here
            }
    
        });
        return entries;
    }
    
    
    //Usage
    var webUrl = "https://xyz.sharepoint.com"; // Need to be changed
    var listId = "{2697fad3-57d8-455c-8a49-59a065b08b31}"; // Need to be changed
    var listItemId = 3; // Need to be changed
    getItemVersions(webUrl, listId, listItemId, function(versionEntries) {
        console.log(versionEntries);
    });
    

    Regards.

    Wednesday, March 15, 2017 9:44 AM
  • Thanks Jaydeep for your reply and the code. This approach requires an extensive HTML parsing to get hold of values. Also it requires string parsing to fetch values of specific columns. Is it the only way to fetch version history?

    I tried SPServices GetVersionCollection operation. The main drawback of the underlying service is, it accepts only one column at a time. But, with one column it works great.

    I have seen people using Version collection using CSOM, but I was specifically looking for JSOM alternative.

    Does it mean Microsoft has not provided an option in JSOM or REST?

    Thursday, March 16, 2017 7:24 AM
  • Hi,

    As per my knowledge, there is no native JSOM or REST api for this currently, so you have to try with custom approach(the solution Jaydeep shared.)

    Or you could try to create custom service for this.

    Here is one link for your reference.

    https://social.technet.microsoft.com/wiki/contents/articles/24194.sharepoint-2013-create-a-custom-wcf-rest-service-hosted-in-sharepoint-and-deployed-in-a-wsp.aspx

    Best Regards,

    Lee


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

    Friday, March 17, 2017 8:13 AM
  • Thanks for the update Lee. I'll mark Jaydeep's post as Answer, as there is no native api in JSOM or REST.
    Friday, March 17, 2017 11:23 AM
  • Hello Jaydeep,

    i have the same requirement but mine is different a little bit, i have a list and it stored an agreement,that agreement is renewed every year,so they need when it is renewed 3 times they need 3 hyperlink and when clicked each,the link goes to the history of that item,

    so what i did is i archive the item to another list using workflow and try to display again to main list.

    so please i need help on this and thanks in advance,

    Best Regards,


    • Edited by ZeZe Assefa Friday, January 11, 2019 4:15 PM
    Friday, January 11, 2019 4:15 PM