How to Get All Folders From Root Site Using REST

Introduction

This article shows how to retrieve all the folders in the list using REST. Develop this project using the following method in the NAPA Tool.

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 Create Site and then choose the Create button.
  • Replace APP.js with the following source code.
  • Publish Your App.



Prerequisites

The following are the important steps to be done before creating the app. 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.


Default ASPX

Add the following div content to the default aspx page in the app.
  1. <div>  
  2.         <p>  
  3.             <b>Retrive Folders</b>  
  4.             <br />  
  5.             <select style="height:500px; width:510px" multiple="multiple" id="allFolders"></select>  
  6.         </p>  
  7. </div 
SourceCode
  1. 'use strict';  
  2. var hostweburl;  
  3. var appweburl;    
  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.     // Resources are in URLs in the form:  
  10.     // web_url/_layouts/15/resource   
  11.     // Load the js file and continue to load the page with information about the folders.  
  12.     // SP.RequestExecutor.js to make cross-domain requests  
  13.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", getFolders);  
  14. });
  15. //Retrieve all of the folders from root Site  
  16. function getFolders() {  
  17.     var executor;
  18.     // Initialize the RequestExecutor with the app web URL.  
  19.     executor = new SP.RequestExecutor(appweburl);  
  20.     executor.executeAsync({  
  21.         url: appweburl + "/_api/SP.AppContextSite(@target)/web/folders?@target='" + hostweburl + "'",  
  22.         method: "GET",  
  23.         headers: {  
  24.             "Accept""application/json; odata=verbose"  
  25.         },  
  26.         success: FoldersSuccessHandler,  
  27.         error: FoldersErrorHandler  
  28.     });  
  29. }    
  30. //Populate the selectFolders control after retrieving all of the folders.  
  31. function FoldersSuccessHandler(data) {  
  32.     var jsonObject = JSON.parse(data.body);  
  33.     var allFolders = document.getElementById("allFolders");  
  34.     if (allFolders.hasChildNodes()) {  
  35.         while (allFolders.childNodes.length >= 1) {  
  36.             allFolders.removeChild(allFolders.firstChild);  
  37.         }  
  38.     }  
  39.     var results = jsonObject.d.results;  
  40.     for (var i = 0; i < results.length; i++) {  
  41.         var Option = document.createElement("option");  
  42.         Option .value = results[i].Name;  
  43.         Option .innerText = results[i].Name;  
  44.         allFolders.appendChild(Option );  
  45.     }  
  46. }  
  47. function FoldersErrorHandler(data, errorCode, errorMessage) {  
  48.     alert("Could not retrieve  Folders: " + errorMessage);  
  49. }  
  50. //Utilities  
  51. // Retrieve a query string value.  
  52. // For production purposes you may want to use a library to handle the query string.  
  53. function getQueryStringParameter(paramToRetrieve) {  
  54.     var params = document.URL.split("?")[1].split("&");  
  55.     for (var i = 0; i < params.length; i = i + 1) {  
  56.         var singleParam = params[i].split("=");  
  57.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  58.     }  

Publish


Output

All Folders from Root Folder are retrieved successfully.