SharePoint 2013 - Get SPListItem versions via REST
Asked Answered
A

5

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?

Aconcagua answered 26/6, 2014 at 6:20 Comment(0)
W
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"));
    });  
  }
}); 
Wartow answered 26/6, 2014 at 11:10 Comment(4)
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.Palatinate
As far as i know, in both approaches versions information could be retrieved per item onlyWartow
@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 characterHeilman
I got an error saying "Item does not exist. It may have been deleted by another user"Ritz
M
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.

Motivate answered 31/10, 2018 at 6:22 Comment(6)
Doesn't work on my SharePoint 2013 server. Can you elaborate on what version of SharePoint this is available?Icehouse
@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.Motivate
This only returns the version metadata, not the record valuesFelicita
@Felicita It returns record values just fine for me. What version of SharePoint are you using?Motivate
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?Felicita
@Felicita 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?Motivate
W
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.

Wino answered 9/3, 2016 at 20:43 Comment(1)
This works for getting the current version of the item, but not the content of that version, or a previous one.Colon
A
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;
Acree answered 9/11, 2017 at 7:44 Comment(0)
B
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/*")
Bobbi answered 6/12, 2017 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.