Ads

Friday, 17 February 2017

Use REST API to play with list & Document library

Here we will use NAPA tool to write the REST scripts.

Open developer site --> Open NAPA office 365 development tool --> Add new project
If you don't have NAPA tool then search and install NAPA App in your site.

In default.aspx page write your HTML like below so that we can trigger the methods as per our need
Ex:
  1. <button id="btn_ID_Here"> Delete Folder </button>  (Here we need jQuery to fire click event) 
  2. <input id="btn_ID_Here" type="button" value="Click" onclick="ButtonClickFunction()"/>  
Now we will replace the default "app.js " file with below codes

------------------------------------------ JS code Start Here --------------------------
'use strict';
var hostweburl;
var appweburl;
 // Get the URLs for the app web the host web URL from the query string.
$(document).ready(function ()
 {
    //Get the URI decoded URLs.
    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
 
    // Resources are in URLs in the form:
    // web_url/_layouts/15/resource  
 
    // Load the js file and continue to load the page with information about the folders.
    // SP.RequestExecutor.js to make cross-domain requests
    $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");

//Below one is optional to bind click function to normal html as in example 1 above
         $("#btn_ID_Here").click(function (event)
        {  
            ButtonClickFunction();  
            event.preventDefault();  
        }); 
});

// ------- Your Custom functions Starts Here
//Button Click Calling Function Here
function ButtonClickFunction()
{
     
}
//1. REST API to Upload file in document library
//2. REST API to Get File versions in document library
//3. REST API to CheckIn file
//4. REST API to Add template file in doc lib
//5. REST API to Checkout file
//6. REST to Delete folder from document library
//7. REST to Create folder in doc lib
//8. REST to Get all folders from root site


// ------- Your Custom functions Ends Here    

function getQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) return singleParam[1];
    }
}
------------------------------------------JS Code End Here --------------------------

Once coding done specify the permissions that your app needs as in the following:
Choose the Properties button at the bottom of the page.

    In the Properties window, choose Permissions.
    In the Content category, set Write permissions for the Tenant scope.
    In the Social category, set Read permissions for the User Profiles scope.
    Close the Properties window.

Publish the App and Click the Trust it Button.









No comments:

Post a Comment

Ads