7

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?

5 Answers5

19

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"));
    });  
  }
}); 
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Any fast way to loop through All items/All Versions? We are looking to report on the current version if any version had a choice field changed to set value. – Hell.Bent Apr 04 '15 at 13:25
  • As far as i know, in both approaches versions information could be retrieved **per item** only – Vadim Gremyachev Apr 04 '15 at 13:29
  • @VadimGremyachev I am trying to fetch details from version history page using your suggestion, but I am getting error at line var versionList = $(data).find('table.ms-settingsframe'); I am getting error as Invalid character – salah9 Aug 05 '16 at 01:26
  • I got an error saying "Item does not exist. It may have been deleted by another user" – Paul Vu May 29 '20 at 16:45
5

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.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • 1
    Doesn't work on my SharePoint 2013 server. Can you elaborate on what version of SharePoint this is available? – Joao Silva May 05 '19 at 10:50
  • 1
    @JoaoSilva This works for me in SharePoint Online. Indeed, it seems this doesn't work in 2013 and I hadn't noticed that the question was specifically about 2013. I don't have a 2016 environment where I can try this. – JLRishe May 05 '19 at 19:56
  • This only returns the version metadata, not the record values – Oxossi Apr 01 '20 at 22:43
  • @Oxossi It returns record values just fine for me. What version of SharePoint are you using? – JLRishe Apr 02 '20 at 02:56
  • I'm using SharePoint Online. May I ask what version are you using? ...And are you getting back prior item content detail (i.e. beyond version# etc) from /versions? – Oxossi Apr 03 '20 at 03:23
  • @Oxossi I didn't notice your question when you posted it. I'm using SharePoint online and I see all of the column values when I use this (even hidden columns). Is it possible you don't have versioning enabled on the list? – JLRishe Oct 05 '22 at 03:54
1

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.

Philippe Lavoie
  • 2,583
  • 5
  • 25
  • 39
  • This works for getting the current version of the item, but not the content of that version, or a previous one. – Alberto S. Sep 14 '17 at 09:52
1

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;
0

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/*")
PMerlet
  • 2,568
  • 4
  • 23
  • 39