Ensure appropriate permission is provided to access the content. Click on Properties button, and then click on Permissions. Set the required permission to access the content.
'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 list templates.
// 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() {
getListTemplates();
}
//Retrieve all of the list templates
function getListTemplates() {
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/listtemplates?@target='" + hostweburl + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: getListTemplatesSuccessHandler,
error: getListTemplatesErrorHandler
});
}
//Populate the selectListTemplates control after retrieving all of the list templates.
function getListTemplatesSuccessHandler(data) {
var jsonObject = JSON.parse(data.body);
var selectListTemplates = document.getElementById("selectListTemplates");
if (selectListTemplates.hasChildNodes()) {
while (selectListTemplates.childNodes.length >= 1) {
selectListTemplates.removeChild(selectListTemplates.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;
selectListTemplates.appendChild(selectOption);
}
}
function getListTemplatesErrorHandler(data, errorCode, errorMessage) {
alert("Could not get List Templates: " + errorMessage);
}