Retrieve All Lists And/or Libraries in SharePoint Using REST API

Introduction

We can use the SharePoint 2013 Representational State Transfer (REST) service to do the same tasks you can do when you use .Net CSOM or JSOM.

Here I will explain how to retrieve all lists and/or libraries in SharePoint 2013 using the REST API.

Procedure

  • On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Retrievelist and then choose the Create button.

Prerequisite

These are the important steps to be done before creating the app.

Specify the following permissions that your app needs:

  • 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.

Expand the Scripts node, choose the App.js file and delete the contents of the file and replace with the following code:

  1. var hostweburl;  
  2. var appweburl;  
  3.   
  4. // Get the URLs for the app web the host web URL from the query string.  
  5. $(document).ready(function () {  
  6.   //Get the URI decoded URLs.  
  7.   hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  8.   appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  9.   
  10.   // Load the SP.RequestExecutor.js file.  
  11.   $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", runCrossDomainRequest);  
  12. });  
  13.   
  14. // Build and send the HTTP request.  
  15. function runCrossDomainRequest() {  
  16.   var executor = new SP.RequestExecutor(appweburl);   
  17.   executor.executeAsync({  
  18.       url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists?@target='" + hostweburl + "'",  
  19.       method: "GET",   
  20.       headers: { "Accept""application/json; odata=verbose" },   
  21.       success: getlistfromsite,   
  22.       error: errorHandler   
  23.   });  
  24. }  
  25.   
  26.   
  27. //retrive all lists from site  
  28.     
  29.  function getlistfromsite(data) {  
  30.         var jsonObject = JSON.parse(data.body);  
  31.         var oists = document.getElementById("lists");  
  32.         if (oists.hasChildNodes()) 
  33.         {  
  34.             while (oists.childNodes.length >= 1) {  
  35.                 oists.removeChild(oists.firstChild);  
  36.             }  
  37.         }  
  38.         var results = jsonObject.d.results;  
  39.         for (var i = 0; i < results.length; i++) 
  40.         {  
  41.             var listcombined = document.createElement("option");  
  42.             listcombined.value = results[i].Title;  
  43.             listcombined.innerText = results[i].Title;  
  44.             oists.appendChild(listcombined);  
  45.         }  
  46.     }  
  47.       
  48.     //error handler  
  49. function errorHandler(){  
  50. alert('error');  
  51. }  
  52.   
  53. // Get a query string value.  
  54. // For production apps, you may want to use a library to handle the query string.  
  55. function getQueryStringParameter(paramToRetrieve) {  
  56.   var params = document.URL.split("?")[1].split("&");  
  57.   //var strParams = "";  
  58.   for (var i = 0; i < params.length; i = i + 1) {  
  59.     var singleParam = params[i].split("=");  
  60.     if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  61.   }  
  62. }   
Once you have done the code part run the project .

Once you have deployed the app select the Click here to launch your app in new window link and it will redirect to another page.



Click the Trust it button here.

Output

Current users followed news from my site.



I hope you have enjoyed this.