Get File's Version in SharePoint Using REST

Develop the project using the following method in the NAPA Tool.

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button.
  • Replace APP.js with the following source code below.
  • Publish Your App.



Prerequisites

These are important steps to be done before creating the app.

Specify the permissions that your app needs as in the following.

Choose the Properties button at the bottom of the page.

  • In the Properties window, choose Permissions.
  • In the Content category, set Write permissions for the Tenant scope.
  • In the Social category, set Read permissions for the User Profiles scope.
  • Close the Properties window. 



Default ASPX

Code Understanding


Note

Endpoint URI

  1. http://<site url>/_api/web/getfilebyserverrelativeurl('/<folder name>/<file name>')/versions  
HTTP methods

This resource supports the following HTTP methods:

GET  |  POST

Source Code
  1. 'use strict';  
  2.   
  3. var hostweburl;  
  4. var appweburl;  
  5.   
  6. // Get the URLs for the app web the host web URL from the query string.  
  7.   
  8. $(document).ready(function()   
  9. {  
  10.   
  11.     //Get the URI decoded URLs.  
  12.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  13.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  14.   
  15.     // Resources are in URLs in the form:  
  16.     // web_url/_layouts/15/resource  
  17.   
  18.   
  19.     // Load the js file and continue to load the page with information about the folders.  
  20.     // SP.RequestExecutor.js to make cross-domain requests  
  21.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", versionfile);  
  22. });  
  23.   
  24.   
  25. //Retrieve all of the folders from root Site  
  26. function versionfile()   
  27. {  
  28.     var executor;  
  29.   
  30.     // Initialize the RequestExecutor with the app web URL.  
  31.     executor = new SP.RequestExecutor(appweburl);  
  32.   
  33.     executor.executeAsync  
  34.     ({  
  35.   
  36.         url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('/sites/apps/Shared Documents/RESTFolder.docx')/versions?@target='" + hostweburl + "'",  
  37.         method: "GET",  
  38.   
  39.   
  40.         headers:
     {  
  41.             "accept""application/json; odata=verbose"  
  42.         },  
  43.         success: SuccessHandlerFileVersions,  
  44.         error: ErrorHandlerFileVersions  
  45.     });  
  46. }  
  47.   
  48. / Success Handler  
  49.     Function SuccessHandlerFileVersions (data)   
  50.     {  
  51.         var FV;  
  52.         var jsonObject = JSON.parse(data.body);  
  53.         var results = jsonObject.d.results;  
  54.         for (var i = 0; i < results.length; i++)   
  55.         {  
  56.             FV += results[i].VersionLabel + '\n';  
  57.         }  
  58.         / / Display the File versions  
  59. alert(FV);  
  60. }  
  61. // Error Handler  
  62. function ErrorHandlerFileVersions(data, errorCode, errorMessage)  
  63. {  
  64.     alert("Could not get the file versions: " + errorMessage);  
  65. }  
  66.   
  67. //Utilities  
  68. // Retrieve a query string value.  
  69. // For production purposes you may want to use a library to handle the query string.  
  70. function getQueryStringParameter(paramToRetrieve)   
  71. {  
  72.     var params = document.URL.split("?")[1].split("&");  
  73.     for (var i = 0; i < params.length; i = i + 1)   
  74.     {  
  75.         var singleParam = params[i].split("=");  
  76.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  77.     }  
  78. }  
Publish

Publish the App and click the Trust it Button.


Output

File Version retrieved successfully.


Up Next
    Ebook Download
    View all
    Learn
    View all