I want to get the file of specific previous versions using java script object model in SharePoint 2013 environment.
Please share the code 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 communityI want to get the file of specific previous versions using java script object model in SharePoint 2013 environment.
Please share the code if any one is already worked on similar requirement.
Thanks,
Kailash
Using JSOM:
You can use the below sample code that uses the restoreByLabel
method to restore the previous version of the document:
<script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext',restoreFile);
});
var oFile;
function restoreFile() {
//Get client context and web
clientContext = new SP.ClientContext();
var oWeb = clientContext.get_web();
//Get File object and restore a specific version
oFile=oWeb.getFileByServerRelativeUrl('/sites/Playground/Demo
Library/Document.docx');
oFile.get_versions().restoreByLabel("6.0");
//Load Client Context and execute the batch
clientContext.load(oFile);
clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
//Get major version
var majorVersion =oFile.get_majorVersion();
console.log("Version Restored . New Major Version - "+ majorVersion );
}
function QueryFailure(sender,args) {
console.log('Request failed - '+args.get_message());
}
</script>
Using REST API:
You can use below endpoint to get all version of file:
siteUrl/_api/Web/GetFileByServerRelativeUrl('fileURL')/Versions