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"));
//Assign events to buttons
$("#createlistbutton").click(function(event) {
createList();
event.preventDefault();
});
// 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 top level folders.
// SP.RequestExecutor.js to make cross-domain requests
$.getScript(scriptbase + "SP.RequestExecutor.js");
});
// 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];
}
}
// Create a new custom list
function createList() {
var listName = document.getElementById("createlistname").value;
var executor;
// Initialize the RequestExecutor with the app web URL.
// Content Type Header is used to tell server what format data in the body contains.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/[email protected]='" + hostweburl + "'", method: "POST",
body: "{ '__metadata': { 'type': 'SP.List' }, 'BaseTemplate': 100,'Description': '" + listName + "', 'Title':'" + listName + "'}",
headers: {
"content-type": "application/json; odata=verbose"
},
success: createListSuccessHandler,
error: createListErrorHandler
});
}
// Success Handler
function createListSuccessHandler(data) {
alert("List Created successfully")
}
// Error Handler
function createListErrorHandler(data, errorCode, errorMessage) {
alert("Could not create a new list: " + errorMessage);
}