Get All File Versions in SharePoint using REST API

The example in this topic show how to use REST API to Get All File Versions in SharePoint

  • function GetAllFileVersions() {
  •  
  •     var WebServerRelativeUrl = _spPageContextInfo.webServerRelativeUrl;
  •  
  •     // Provide Internal name of the library here
  •     var DocuentLibraryInternalName = "Document%20Library";
  •  
  •     // Provide name of the document
  •     var DocumentName = "Test%20Doc.docx";
  •  
  •     var ServerRelativeUrlofFile = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('" + WebServerRelativeUrl + "/" + DocuentLibraryInternalName + "/" + DocumentName + "')"
  •  
  •     $.ajax
  •         ({
  •             // _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
  •             // You can replace this with other site URL where you want to apply the function
  •  
  •             url: ServerRelativeUrlofFile + "/versions",
  •             type: "GET",
  •             headers:
  •                {
  •                    // Accept header: Specifies the format for response data from the server.
  •                    "Accept": "application/json;odata=verbose"
  •                },
  •             success: function (data, status, xhr) {
  •  
  •                 var dataresults = data.d.results;
  •                 for (var i = 0; i < dataresults.length; i++) {
  •                     console.log("Version Label - " + dataresults[i]["VersionLabel"] + " , Version Comment - " + dataresults[i]["CheckInComment"]);
  •                 }
  •             },
  •             error: function (xhr, status, error) {
  •                 console.log("Failed");
  •             }
  •         });
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint