I want to get the previous version file using java script object model in SharePoint 2013 environment.
Please guide me if any one is already worked on similar requirement.
Thanks,
Kailash
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityYou can get the specific file's version using REST call by below given code.
// Get the file versions
function getFileVersions() {
var getfileurl = document.getElementById("getfileurl").value;
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('" + getfileurl + "')/Versions?@target='" + hostweburl + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: getFileVersionsSuccessHandler,
error: getFileVersionsErrorHandler
});
}
// Success Handler
function getFileVersionsSuccessHandler(data) {
var versions = 'File Versions:\n'
var jsonObject = JSON.parse(data.body);
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
versions += results[i].VersionLabel + '\n';
}
// Display the File versions
alert(versions);
}
// Error Handler
function getFileVersionsErrorHandler(data, errorCode, errorMessage) {
alert("Could not get the file versions: " + errorMessage);
}
In SharePoint 2013, I suggest you use REST API to achieve it. The following example for your reference, modify the version label and file url in the code.
<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var versionLabel="3.0";
var fileRelativeUrl=_spPageContextInfo.webServerRelativeUrl+"/ABC/sample.txt";
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('"+fileRelativeUrl+"')/Versions?$filter=VersionLabel eq '"+versionLabel+"'",
type: "GET",
headers: {
"Accept": "application/json;odata=verbose",
},
success: function (data){
if(data.d.results.length>0){
var versionFileUrl=_spPageContextInfo.webServerRelativeUrl+"/"+data.d.results[0].Url;
$("#previousVersionFile").append("<a href='"+versionFileUrl+"'>Previous Version</a>");
}
},
error: function (data) {
//alert("Error");
}
});
})
</script>
<div id="previousVersionFile"/>