3

I need to get all field values of an specific version.

It is possible by sharepoint hosted app (javascript)?

I need to create a custom version history page of itens.

If there is a caml query solution, i think that is the best option.

3 Answers 3

11

It is not supported to retrieve 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>

and extract versions info from page content as demonstrated below:

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


function parseVersionsData(data){
       var entries = {};
       var versionList = $(data).find('table.ms-settingsframe');


       versionList.find('> tbody > tr').each(function(i){
         if(i > 0 && (i) % 2 == 0) {
            var verRow = $(this); //get version row
            var propsRow = verRow.next(); //get properties row
            var versionLabel = verRow.find('td:first').html().trim();
            if(versionLabel.length > 0) {
              entries[versionLabel] = {};
              //extract item version properties
              propsRow.find('tr').each(function(i){
                var pName = $(this).find('td:nth-child(1)').text().trim();
                var pVal = $(this).find('td:nth-child(2)').text().trim();
                entries[versionLabel][pName] = pVal; 
              });
            }      
         }

       });   
       return entries;
}

Usage

var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listId = _spPageContextInfo.pageListId;
var itemId = parseInt(GetUrlKeyValue('ID'));
getItemVersions(webUrl,listId,itemId,function(versionEntries){
      console.log(versionEntries);
    });

Using 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"));
    });  
  }
}); 
3
  • Downvote for the screen scraping ;) upvote for the Lists.asmx ;)
    – michalh
    Commented Nov 14, 2016 at 9:30
  • sharepoint saves a lot of versions if you touch a cell in Quick Edit mode...
    – jpfreire
    Commented Feb 15, 2017 at 18:06
  • Not working for me . #1 gets the information cut. If the value is too long, it shows "...". I also had to use RequestExecutor to not get a CORS error . For #2 , I get an error of CORS. Any ideas? I am working in a provider hosted app.
    – Flezcano
    Commented Jul 28, 2017 at 18:14
4

Client Object Model doesn't have the property to retrieve the ListItem Version details.

Instead SharePoint web service provide the option to retrieve the version details of a list item. We can use the below options to get the version details.

SharePoint WebService to get Version Collection

SPservices to retrieve version history of items from SharePoint ListItem

-1

Get old version of list item :

foreach (SPListItemVersion version in spItem.Versions)
{
    //code to fetch field values
}
1
  • 1
    This is not CSOM or JSOM, but SSOM
    – michalh
    Commented Nov 14, 2016 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.