How To Get All SharePoint Lists Using REST API In SharePoint Online And Office 365

Welcome to an article on “How to get all SharePoint Lists using REST API in SharePoint Online and Office 365” where we will see the steps of creating an app using Napa Tool which will help us to view all the lists on our site using REST API.

  • Open the “NAPA” Office 365 Development Tools through your SharePoint store.
  • Click on Add New Project.

  • It will ask you, what type of app do you want to build?

    add

  • Select SharePoint Add-in and provide a name to your app and click on Create.

    add in

  • You will see the screen below on the app.

    content

  • Click on Default.aspx and paste the code below under the, “<asp:ContentContentPlaceHolderID="PlaceHolderMain" runat="server">”.

    Code:
    1. <div>  
    2.     <p>  
    3.         <b>All Lists</b>  
    4.         <br />  
    5.         <select style="height:200px; width:200px" multiple="multiple" id="findlist"></select>  
    6.     </p>  
    7. </div>  
  • Now on the navigation click on the App.js file and paste the code below removing the previous code completely.

    Code:
    1. 'use strict';  
    2. var hostweblink;  
    3. var applink;  
    4. // Fetch values on App Load  
    5. $(document)  
    6.     .ready(function()  
    7.         {  
    8.         hostweblink = decodeURIComponent(  
    9.             getQueryStringval("SPHostUrl"));  
    10.         applink = decodeURIComponent(  
    11.             getQueryStringval("SPAppWebUrl"));  
    12.   
    13.         varscriptlink = hostweblink + "/_layouts/15/";  
    14.         $.getScript(scriptlink + "SP.RequestExecutor.js", loadPage);  
    15.     });  
    16. //Retrieve the clean URL  
    17. function getQueryStringval(paramToRetrieve)  
    18. {  
    19.         var paramval = document.URL.split("?")[1].split("&");  
    20.         for (vari = 0; i < paramval.length; i = i + 1)  
    21.         {  
    22.             var param1 = paramval[i].split("=");  
    23.             if (param1[0] == paramToRetrieve) return param1[1];  
    24.         }  
    25.     }  
    26.     //Retrieve all lists on the page load through the function  
    27. function loadPage()  
    28. {  
    29.     getalllist();  
    30. }  
    31. function getalllist()  
    32. {  
    33.         var play;  
    34.         play = new SP.RequestExecutor(applink);  
    35.         play.executeAsync({  
    36.             url: applink + "/_api/SP.AppContextSite(@target)/web/Lists?@target='" + hostweblink + "'",  
    37.             method: "GET",  
    38.             headers:  
    39.           {  
    40.                 "Accept""application/json; odata=verbose"  
    41.             },  
    42.             success: getlistcompleted,  
    43.             error: getlistfailed  
    44.         });  
    45.     }  
    46.     //Populate the selectLists control after retrieving all of the site's Lists.  
    47. functiongetlistcompleted(data)  
    48. {  
    49.     varjsonobjectbody = JSON.parse(data.body);  
    50.     varfindlists = document.getElementById("findlist");  
    51.     if (findlists.hasChildNodes())  
    52.     {  
    53.         while (findlists.childNodes.length >= 1)  
    54.         {  
    55.             findlists.removeChild(findlists.firstChild);  
    56.         }  
    57.     }  
    58.     var results = jsonobjectbody.d.results;  
    59.     for (var i = 0; i < results.length; i++)  
    60.     {  
    61.         var selectOption = document.createElement("option");  
    62.         selectOption.value = results[i].Title;  
    63.         selectOption.innerText = results[i].Title;  
    64.         findlists.appendChild(selectOption);  
    65.     }  
    66. }  
    67.   
    68. function getlistfailed(data, errorCode, errorMessage)  
    69. {  
    70.     alert("Could not load the lists :" + errorMessage)  
    71. }  
  • Click on the settings icon on the tool on the left.

    tool

  • Under the properties, select Permissions and provide full control to the app on the Site Collection level.

    permission

  • Click on the deploy button on the left and run the project.

    run

  • Click on the launch button.

    launch

  • Accept the trust and click on ‘Trust It’.

    trust it

  • Your app will be deployed and open for you as per the following screenshot:

    get all list

  • Your entire lists will load on your app on the page load of the app.

Here we saw today how to get all SharePoint Lists using REST API in SharePoint Online and Office 365. You will love your app. Keep reading and keep learning.

Read more articles on SharePoint: