Ads

Friday, 17 February 2017

REST API to retrieve list/libraries from sharepoint

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
-----------------------
$(document).ready(function () {
  //Get the URI decoded URLs.
  hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
  appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
 
  // Load the SP.RequestExecutor.js file.
  $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);
});
 
// Build and send the HTTP request.
function runCrossDomainRequest() {
  var executor = new SP.RequestExecutor(appweburl);  
  executor.executeAsync({
      url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",
      method: "GET",  
      headers: { "Accept": "application/json; odata=verbose" },  
      success: getlistfromsite,  
      error: errorHandler  
  });
}    
//retrive all lists from site    
 function getlistfromsite(data) {
        var jsonObject = JSON.parse(data.body);
        var oists = document.getElementById("lists");
        if (oists.hasChildNodes())
        {
            while (oists.childNodes.length >= 1) {
                oists.removeChild(oists.firstChild);
            }
        }
        var results = jsonObject.d.results;
        for (var i = 0; i < results.length; i++)
        {
            var listcombined = document.createElement("option");
            listcombined.value = results[i].Title;
            listcombined.innerText = results[i].Title;
            oists.appendChild(listcombined);
        }
    }
     
    //error handler
function errorHandler(){
alert('error');
}

No comments:

Post a Comment

Ads