0

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

0

You 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);
    }
  • My requirement is to get the specific version file. Example- a file has 3.4 as latest version, but I want to get the file with version 3.0. – Kailash Sahoo Sep 2 at 15:39
  • then use filter in REST API – Milind Sep 3 at 5:49
0

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"/>

Article: Working with folders and files with REST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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