Ads

Friday, 17 February 2017

REST API to get file versions

Hope you have idea how to create SharePoint App.
If you are using NAPA tool then replace below codes in APP.js file.
So i am just focusing on the REST script part to do the activities

I recommend to go through REST API for SP document libraries before proceed.

HTML file in Default.aspx 

----------------------
JS script in app.js (Paste the below methods in app.js file) Refer here
-----------------------
function versionfile()  
{
    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('/sites/apps/Shared Documents/RESTFolder.docx')/versions?@target='" + hostweburl + "'",
        method: "GET",
 
 
        headers:
 {
            "accept": "application/json; odata=verbose"
        },
        success: SuccessHandlerFileVersions,
        error: ErrorHandlerFileVersions
    });
}
 
/ Success Handler
    Function SuccessHandlerFileVersions (data)  
    {
        var FV;
        var jsonObject = JSON.parse(data.body);
        var results = jsonObject.d.results;
        for (var i = 0; i < results.length; i++)  
        {
            FV += results[i].VersionLabel + '\n';
        }
        / / Display the File versions
alert(FV);
}
// Error Handler
function ErrorHandlerFileVersions(data, errorCode, errorMessage)
{
    alert("Could not get the file versions: " + errorMessage);
}   

No comments:

Post a Comment

Ads