Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2013 - Get SPListItem versions via REST

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?

like image 938
Nikolay Zainchkovskiy Avatar asked Jun 26 '14 06:06

Nikolay Zainchkovskiy


3 Answers

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"));
    });  
  }
}); 
like image 62
Vadim Gremyachev Avatar answered Sep 21 '22 20:09

Vadim Gremyachev


Note: This doesn't seem to work in 2013. I have verified this working in SharePoint Online and it may work in 2016+ but I have not verified the latter.

The situation may have changed since this question was originally posted, but it is now possible to use the REST API to get version history for any list/library item:

https://url/to/site/_api/web/Lists/getbytitle('MyListName')/items(ITEMID)/versions

This will return a series of results for the current version and all past versions, with the item's column values from each version.

As with other REST endpoints, you can use $select, $filter, etc. to further manipulate the results.

like image 38
JLRishe Avatar answered Sep 17 '22 20:09

JLRishe


In the REST API, you can select the property OData__UIVersionString. It also supports OData__ModerationStatus

Ex:

GET http://site url/_api/web/lists/GetByTitle(‘Test')/items(item id)?$select=OData__UIVersionString,OData__ModerationStatus

More infos : https://msdn.microsoft.com/en-us/library/office/dn292552.aspx

It's not a solution to get all the versions or a specific version, but it's more info on the version.

like image 22
Philippe Lavoie Avatar answered Sep 19 '22 20:09

Philippe Lavoie