0

Using the following JavaScript function, I am able to get all versions of a specified list item:

function getListItemVersions(listTitleOrId, listItemId) {
  let context = null;
  let web = null;
  try {
    context = SP.ClientContext.get_current();
    web = context.get_web();
    let filePath = `/Lists/${listTitleOrId}/${listItemId}_.000`;
    let listItemInfo = web.getFileByServerRelativeUrl(filePath);
    let listItemFields = listItemInfo.get_listItemAllFields();
    context.load(web);
    context.load(listItemInfo);
    context.load(listItemFields);
    context.executeQueryAsync(
      function (sender, args) {
        let fileVersions = listItemInfo.get_versions();
        context.load(fileVersions);
        context.executeQueryAsync(
          function (sender, args) {
            let objlistVersionEnumerator = fileVersions.getEnumerator();
            while (objlistVersionEnumerator.moveNext()) {
              let objCurrentListItemVersion =
                objlistVersionEnumerator.get_current();
              console.log(objCurrentListItemVersion);
            }    
          },
          function (sender, args) {
            console.log('Error getting versions from server', sender, args);
          }
        )    
      }, 
      function (sender, args) {
        console.log('Error getting list from server', sender, args);
      }
    );
  } catch(e) {
  }
}

For example, if I have a list called "Test List" which has an item with an ID of 23, I can call getListItemVersions("Test List", 23) and have all versions of the item logged. The problem is that I'm getting back objects of the type SP.FileVersion, i.e.:

enter image description here

Using the SP.FileVersion returned, how can I get the values of the fields at each version of the list item?

0

While this doesn't necessarily answer my question as asked, I did find an alternate method which allowed me to access all versions of a specific field from a specific list item by, modifying some good code from here.

function getListItemVersions(listWebUrl, listName, listItemId, fieldName,
    onSuccess, onError) {
  var glisoapdef = new jQuery.Deferred();
  var soapHeader = 'http://schemas.microsoft.com/sharepoint/soap/';
  var soapAction = soapHeader + 'GetVersionCollection';
  var soapBody = '<soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-'+
    'instance\' xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' xmlns:soap=\'' +
    'http://schemas.xmlsoap.org/soap/envelope/\'><soap:Body>' + 
    '<GetVersionCollection xmlns="' + soapHeader + '"><strlistID>' + listName +
    '</strlistID><strlistItemID>' + listItemId + '</strlistItemID><strFieldName>'+
    fieldName + '</strFieldName></GetVersionCollection></soap:Body></soap:Envelope>';

  $.ajax({
    url: listWebUrl + '/_vti_bin/lists.asmx',
    type: 'POST',
    async: false,
    beforeSend: function beforeSend(xhr) {
      xhr.setRequestHeader('SOAPAction', soapAction);
    },
    dataType: 'xml',
    data: soapBody,
    contentType: "text/xml;charset='utf-8'",
    complete: function complete(xData, status) {
      var historyOptions = [];
      $(xData.responseText).find('Version').each(function (i) {
        var editorValue = '';
        var editor = $(this).attr('editor').split('#');
        if (editor.length >= 1) {
          editorValue = editor[1];
          var editorLen = editorValue.length;
          if (editorValue.lastIndexOf(',') == editorLen - 1) {
            editorValue = editorValue.substring(0, editorLen - 1);
          }
        }
        editor = editorValue.replace(',,', ',');
        historyOptions.push({
          Editor: editor,
          Modified: $(this).attr('Modified'),
          FieldValue: $(this).attr(fieldName)
        });
      });
      onSuccess(historyOptions);
      glisoapdef.resolve(historyOptions);
    },
    error: function error(jqXHR, textStatus, errorThrown) {
      onError(jqXHR, textStatus, errorThrown);
      glisoapdef.reject(jqXHR, textStatus, errorThrown);
    }
  });
}

You can use it as follows:

getListItemVersions('https://mycompany.sharepoint.com', 'List Name', 2308,
    'Field Name', function(items) {
  console.log(items);
  // do something with the items
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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