0

I made a comments column, enabled "Append changes to existing text", and enabled "Create a version each time you edit an item in this list?". How can I retrieve all versions so I can get all comments using JavaScript?

I tried using the following REST API endpoint:

.../_api/web/GetFileByServerRelativeUrl('.../Lists/Announcements_list/381_.000')/versions

But it doesn't return the comment and the author:

{
d: {
     results: Array(1)
          0:
           CheckInComment: ""
           Created: "2020-06-01T16:41:26Z"
           CreatedBy: {__deferred: {…}}
           ID: 6144
           IsCurrentVersion: false
           Size: 0
           Url: "_vti_history/6144/Lists/Announcements_list/381_.000"
           VersionLabel: "12.0"
           __metadata: {id: ".../_api/SP.FileVersion4f29f152-200e- 
           4773- 
           906a-e4c496699174", uri: ".../_api/SP.FileVersion4f29f152- 
           200e-4773-906a-e4c496699174", type: "SP.FileVersion"}
  }
}

Why is CheckInComment an empty string and why doesn't it get the CreatedBy value?

1 Answer 1

0

You could check the script to get version histories from version page.

https://github.com/shawnadrockleonard/development-samples/blob/25c8f0449985ad9f73e5d3d26c7ad0bdf2d2437f/Sample06/AppFiles/SiteAssets/js/SampleVersionDisp.js#L160

function getListItemByVersionPage(listWebUrl, listId, listItemId, fieldName) {

    var glibvpdef = new jQuery.Deferred();

    var versionsUrl = listWebUrl + '/_layouts/versions.aspx?list=' + listId + '&ID=' + listItemId;

    jQuery.get(versionsUrl)
     .done(function (data) {

         var entries = [];
         var versionList = $(data).find('table.ms-settingsframe');
         if (typeof (versionList) !== typeof (undefined) && versionList !== null) {

             versionList.find('tbody > tr').each(function (i, trval) {
                 // pulls every 2 rows
                 if (i > 0 && (i - 1) % 2 == 0) {
                     try {
                         var verRow = $(this); //get version row
                         var versionLabel = verRow.find('td:first').html().trim();
                         var versionDateElement = verRow.find('table[ctxname=\"ctxVer\"] a'); // Date for modification
                         var versionUserElement = verRow.find('.ms-imnSpan a:nth-child(2)'); // User for Modification
                         if (versionLabel !== ""
                             && (versionDateElement !== null && versionDateElement.length > 0)
                             && (versionUserElement !== null && versionUserElement.length > 0)) {
                             var versionDate = versionDateElement.html().trim();
                             var versionUser = versionUserElement.html().trim();

                             var propsRow = verRow.next(); //get properties row
                             var properties = propsRow.find("table[role=\"presentation\"] tr").map(function (index, val) {

                                 var trproperty = jQuery(val);
                                 if (val.id.indexOf(fieldName) !== -1) {
                                     var entryProperties = {
                                         id: val.id,
                                         title: trproperty.find("td:first").html().trim(),
                                         FieldValue: trproperty.find("td:nth-child(2)").html().trim()
                                     };
                                     return (entryProperties);
                                 }
                             });
                             var entry = {
                                 Label: versionLabel,
                                 Modified: versionDate,
                                 Editor: versionUser,
                                 vprops: properties
                             };
                             entries.push(entry);
                         }
                     } catch (error) {
                         console.log("parse error " + error.message);
                     }
                 }
             });
         }
         glibvpdef.resolve(entries);
     })
    .fail(function (sender, args) {
        glibvpdef.reject(sender, args);
    });

    return glibvpdef.promise();
}
2
  • I tried this and it seems to work! But how do I actually get the comment values? The entry object seems to only return the versionLabel, versionDate, and versionUser.
    – Paul Vu
    Jun 3, 2020 at 14:02
  • It's DOM nodes, you just need phrase the nodes to get comment values. verRow.find(xxx)
    – Lee
    Jun 5, 2020 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.