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.
- <div>
- <p>
- <b>Retrive Folders</b>
- <br />
- <select style="height:500px; width:510px" multiple="multiple" id="allFolders"></select>
- </p>
- </div
SourceCode
- 'use strict';
- var hostweburl;
- var appweburl;
-
- $(document).ready(function () {
-
- hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
- appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
-
-
-
-
- $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", getFolders);
- });
-
- function getFolders() {
- var executor;
-
- executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync({
- url: appweburl + "/_api/SP.AppContextSite(@target)/web/folders?@target='" + hostweburl + "'",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- success: FoldersSuccessHandler,
- error: FoldersErrorHandler
- });
- }
-
- function FoldersSuccessHandler(data) {
- var jsonObject = JSON.parse(data.body);
- var allFolders = document.getElementById("allFolders");
- if (allFolders.hasChildNodes()) {
- while (allFolders.childNodes.length >= 1) {
- allFolders.removeChild(allFolders.firstChild);
- }
- }
- var results = jsonObject.d.results;
- for (var i = 0; i < results.length; i++) {
- var Option = document.createElement("option");
- Option .value = results[i].Name;
- Option .innerText = results[i].Name;
- allFolders.appendChild(Option );
- }
- }
- function FoldersErrorHandler(data, errorCode, errorMessage) {
- alert("Could not retrieve Folders: " + errorMessage);
- }
-
-
-
- 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];
- }
- }
Publish
Output
All Folders from Root Folder are retrieved successfully.