SharePoint 2013 - Get SPListItem versions via REST

Nikolay Zainchkovskiy picture Nikolay Zainchkovskiy · Jun 26, 2014 · Viewed 30k times · Source

I have a SharePoint 2013 List with versioning enabled. I need to to get SPListItem versions list via REST. I can get SPListItem by that request: http://spbreportportal/Projects/_api/lists/getbytitle('Projects')/Items(1) But I can't find in documentation and in response how to retrieve all versions of this item. Is it possible?

Answer

Vadim Gremyachev picture Vadim Gremyachev · Jun 26, 2014

It does not seem possible to get versions for a List Item via REST/CSOM APIs, but there are alternative options

Using Versions.aspx application page

The idea is to perform a get request to Versions page: http://<server>/<site>/_layouts/versions.aspx?list={litsID}&ID=<itemID>

function getItemVersions(url,listId,itemId,success)
{
   var versionsUrl = url + '/_layouts/versions.aspx?list=' + listId + '&ID=' + itemId;  
   $.get( versionsUrl, function( data ) {
      var versionEntries = parseVersionList(data);
      success(versionEntries);
   });
}


function parseVersionList(data){
   var entries = {};
   var versionList = $(data).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();
        entries[versionLabel] = {};
        //extract item properties from propsRow goes here
        //...
     }

   });   
   return entries;
}


//Usage
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listId = _spPageContextInfo.pageListId;
var listItemId = 1;
getItemVersions(webUrl,listId,listItemId,function(versionEntries){
  console.log(versionEntries);
});

Using Lists SharePoint Web Services

Another option would be to utilize Lists SharePoint Web Services that exposes Lists.GetVersionCollection Method to return version information for the specified field in a SharePoint list

SPServices example:

$().SPServices({
  operation: "GetVersionCollection",
  async: false,
  strlistID: "Projects",
  strlistItemID: 1,
  strFieldName: "Description",
  completefunc: function (xData, Status) {
    $(xData.responseText).find("Version").each(function(i) {
      console.log("Name: " + $(this).attr("Description") + " Modified: " + $(this).attr("Modified"));
    });  
  }
});