Nikolay Zainchkovskiy
Nikolay Zainchkovskiy

Reputation: 474

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?

Upvotes: 7

Views: 36472

Answers (5)

JLRishe
JLRishe

Reputation: 101730

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.

Upvotes: 5

To add more information reagrding on how to obtain all version history from a SharePoint list:

//Get ID of the Dossier in SP list
strID = items(i - 1).getAttribute("ows_ID")
Debug.Print strID

//Get all Versions of the ID in SP list as a XML
URL1: https://path to site collection/_vti_bin/owssvr.dll?Cmd=Display&List={LIstID}&XMLDATA=TRUE&Query=*&IncludeVersions=TRUE
XDoc3.Load (URL1 & "&FilterField1=ID&FilterOp1=eq&FilterValue1=" & strID)
Set Item = XDoc3.SelectNodes("//rs:data/*")
Set temp3 = XDoc3.SelectNodes("//rs:data/*")

Upvotes: 0

Albert te Boekhorst
Albert te Boekhorst

Reputation: 11

To add to @Vadim Gremyachev's Excellent answer to use "GetversionCollection": This interface can also be reached using old school SOAP. Unfortunately it only returns one field at the time (so we use a lot of calls ...). The C# snippet is below.

        //https://blogs.msdn.microsoft.com/pinch-perfect/2016/06/04/sharepoint-web-services-read-version-history-for-column-changes/
        //http://www.indy.gov/eGov/City/DCE/Permits/Signs/_vti_bin/lists.asmx?op=GetVersionCollection
        //https://www.codeproject.com/Articles/26338/Using-the-GetListItems-GetVersionCollection-and-Up
        string strSite = 
        string strListGuid = 
        string strListItemID = 
        string strFieldName = "Title" // or some other field name
        string requestXML = "<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='http://schemas.microsoft.com/sharepoint/soap/'>" +
                            "<strlistID>"+ strListGuid + "</strlistID><strlistItemID>" + strListItemID + "</strlistItemID>" +
                            "<strFieldName>"+ strFieldName +"</strFieldName>" +
                            "</GetVersionCollection>" +
                            "</soap:Body>" +
                            "</soap:Envelope>";
        object xmlRequestObj = Activator.CreateInstance(Type.GetTypeFromProgID("Microsoft.XMLHTTP"));
        MSXML2.XMLHTTP xmlRequest = (MSXML2.XMLHTTP)xmlRequestObj;
        xmlRequest.open("Get", strSite + "/_vti_bin/Lists.asmx", false, null, null);
        xmlRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetVersionCollection");
        xmlRequest.send(requestXML);
        string responseText = xmlRequest.responseText;

Upvotes: 1

Philippe Lavoie
Philippe Lavoie

Reputation: 2593

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.

Upvotes: 1

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59348

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"));
    });  
  }
}); 

Upvotes: 20

Related Questions