Friday, June 13, 2014

How to get all versions of a sharepoint list using JSOM

There are many scenarios where the user wants to retrieve the SharePoint list item versions. In SharePoint 2013 world, Microsoft recommends to use client side libraries  to do most of the operations.

In this post we will see how we can retrieve SharePoint list item version using client side SharePoint libraries and especially using JSOM

Here are the steps I used to retrieve list item version using JSOM

Step 1:

Get the list name and try to retrieve the list items data for which you need the versions

spList name is the SharePoint list from which you want to retrieve the list items

camlQuery helps in getting the required data from the sharepoint list

SP.SOD.executeFunc(key, functionName, fn)  - Refer to the following link for more information on  this function

Step 2:

The below code gives the versions of all the items for the given list name.

The below steps explains the above code in more detail
Get the reference to SharePoint context and the below code will show the snippet for getting sharepoint context

Get the reference to current sharepoint web  and the list with the help of sharepoint context reference that we got from the above code snippet

Get individual list items. Fire an async query and get individual list items.

Every list item in sharepoint lists is like url and any list item will be represented as 'path to site collection/Lists/list name/'+id+'_.000'.  id represents the current list id
Get reference to individual list item info with the following code snippet
web.getFileByServerRelativeUrl(filePath) and execute the code 

get_versions() api will give all the versions of the current list item.  
Execute the query async again after calling the function get_versions().

 get_url() will have the information regarding the version history number of the list item 

Once versioning has been enabled, version data can be accessed from a special virtual directory called _vti_history
Refer the following url for more information on version number for the sharepoint list items
Once we get the list item version, the other obvious questions was, how to get the data associated to that version.
There are api's available to get the list data associated to that history version and what I found was, accessing the list item from the version history will always give me file not found error. So the only way to get the list item data for that version number is to use some html scrapping and by using some jquery syntax
Version history data of a particular list item can be accessed from a sharepoint url as shown below
Site collection complete url/Lists/ListName/DispForm.aspx?ID=265&VersionNo=1024
ID = 265 represents the current list item id and VersionNp 1024 represents major version 2 of the list item
In that page, list item data are enclosed with in a table with a called "ms-formtable" and with the help of some jquery we can get all the data from that class.
I have used the below jQuery code to get all the list item data



  1. This code is not working...
    function (sender, args) {
    var fileVersions = listItemInfo.get_versions();
    From here its not getting executed

  2. I'm using your html scrape method to get version field values and this is fine. However, if I use the same method to get the current version (No VersionID) I do not get any field values

  3. Hi, I am unable to get createdBy.get_title() of SP.FileVersion object even I tried to load createdBy.get_title() but failed.