0

I want to make rest call or implement some JSOM mechanism to retrieve previous version file from _vti_history.

For example: _vti_history/1024/Documents/Filename.docx

Please guide me if this is possible in some way.

Thanks,

Kailash

1 Answer 1

3

You can use below to get file versions history, there is rest endpoint available which will give you all version of a file using item id, also it can be done using JSOM.

REST API

/sites/yoursite/_api/Web/Lists(@v0)/Items(@v1)/Versions?&@v0=guid'112313-13cebe-1233-822e-asd123123123'&@v1=10

JSOM (ref link)

var oItems, clientContext, oFile, oFileVersions;
function GetAllVersions() {
    // You can optionally specify the Site URL here to get the context
    // If you don't specify the URL, the method will get the context of the current site
    // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
    clientContext = new SP.ClientContext();

    // Get List Object
    var olistCollection = clientContext.get_web().get_lists();
    var oList = olistCollection.getByTitle("Documents");

    // Get Item using CAML Query
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'FileLeafRef\'/>' + '<Value Type=\'File\'>Document.docx</Value></Eq></Where></Query></View>');
    oItems = oList.getItems(camlQuery);

    // Load List Item collection object
    clientContext.load(oItems);

    // Execute the query to the server.
    clientContext.executeQueryAsync(onsuccess, onfailed);
}

function onsuccess() {
    // Loop through each Item.
    var itemCount = oItems.get_count();
    for (var i = 0; i <= itemCount - 1; i++) {
        // Get File object of the list item
        oFile = oItems.itemAt(i).get_file();
        oFileVersions = oFile.get_versions();
        clientContext.load(oFileVersions);

        break;
    }

    //Execute the batch
    clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);
}

function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

function FinalQuerySuccess(sender, args) {

    // Iterate through Enumerator
    var oEnumerator = oFileVersions.getEnumerator();
    while (oEnumerator.moveNext()) {
        var oFile = oEnumerator.get_current();
        console.log("Version Label : " + oFile.get_versionLabel());
        console.log("Version Comment : " + oFile.get_checkInComment());
    }
}

function FinalQueryFailure(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}
1
  • Your code gives all the version labels for the file.my requiremet is to get eaxct file of any specific version.Any idea to get it Sep 5, 2019 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.