Wednesday, January 13, 2016

Display Pages Version Information in SharePoint 2013


In SharePoint 2013, I had a requirement to display page versions in drop down and on selection of each version respective version information should open in new window.
This requirement consists of two parts.
  1.          Display drop down  on each page
  2.          Add versions in drop down and on selection of each drop down open version information.

For achieving 1st part, I created page layout and added html Drop down code on page layout source, so every page created using that page layout will have drop down on this page.

Code:

 
               










For 2nd part – I added below script in page layout. This code extracts version number and sets it as text for drop down option and value as URL for opening version information.
In SharePoint under layouts directory we can get all versions information on Versions.aspx.
I am calling this page using JQuery Ajax API to read page content. Once we get all html source which will have all versions information, I am extracting required values using JQuery.
So on drop down change you have to open URL in value field in new window. That’s it.
This code requires {LIST ID} as Pages library ID and Item ID is Page ID.  



var VersonNumberArray = new Array;
$(document).ready(function() {
 getItemVersions(function(versionEntries){
  for ( i = 0; i < versionEntries.length; i++ )
  {
    var opt = document.createElement("option");      
     opt.text = VersonNumberArray[i];
   
    var urlVal = versionEntries[i].href
       
        if (urlVal.indexOf("?") > -1)
        {
          urlVal = urlVal.split('=')[1];
          urlVal = "/Pages/Forms/DispForm.aspx?ID={ItemID}&VersionNo=" + urlVal;
        }
        else
        {
         urlVal = "/Pages/Forms/DispForm.aspx?ID={ItemID}&VersionNo="
        }
       
        opt.value = urlVal;
        document.getElementById('versionid').options.add(opt);
  }


});

 
  });


function getItemVersions(success)
{
var itemid =  document.getElementById('pageid').value;
var versionsUrl = 'http://{SiteURL}/_layouts/15/versions.aspx?list={ListID}&ID='+ itemid;
   $.get( versionsUrl, function( data ) {
 
      var versionEntries = parseVersionList(data);
      success(versionEntries);
   });
 
   }


function parseVersionList(data){
 
   var versionList = $(data).find('table.ms-settingsframe');
   var verurlList =  $(versionList).find('a[onfocus="OnLink(this)"]');
   var versonRows = $(versionList).find('> tbody > tr');
 
   var children = null;
 
   for(var i=0; i < versonRows.length ; i++)
   {
    var td = $(versonRows[i]).children('td:first');
   
     if(td.length == 1)
     {
       if(td[0].vAlign == "top" && td[0].className == "ms-vb2")
        {
          VersonNumberArray.push(td[0].outerText);
        }
     }
    }
    return verurlList;
}

Happy SharePointing
 

No comments:

Post a Comment