'use strict';
var hostweburl;
var appweburl;
// Load the required SharePoint libraries.
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
// Resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js file and continue to load the page with information about the folders.
// SP.RequestExecutor.js to make cross-domain requests
$.getScript(scriptbase + "SP.RequestExecutor.js", loadPage);
});
//Utilities
// Retrieve a query string value.
// For production purposes you may want to use a library to handle the query string.
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];
}
}
function loadPage() {
getFolders();
}
//Retrieve all of the folders
function getFolders() {
var executor;
// Initialize the RequestExecutor with the app web URL.
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: getFoldersSuccessHandler,
error: getFoldersErrorHandler
});
}
//Populate the selectFolders control after retrieving all of the folders.
function getFoldersSuccessHandler(data) {
var jsonObject = JSON.parse(data.body);
var selectFolders = document.getElementById("selectFolders");
if (selectFolders.hasChildNodes()) {
while (selectFolders.childNodes.length >= 1) {
selectFolders.removeChild(selectFolders.firstChild);
}
}
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
var selectOption = document.createElement("option");
selectOption.value = results[i].Name;
selectOption.innerText = results[i].Name;
selectFolders.appendChild(selectOption);
}
}
function getFoldersErrorHandler(data, errorCode, errorMessage) {
alert("Could not get Folders: " + errorMessage);
}