Below is some sample code of how to provision a list from the app web via SharePoint JavaScript object model.
function CreateList(title, url, templateType, hidden) {
//Using the App Web as the client context
clientContext = new SP.ClientContext.get_current();
//Get the host web URL from the query string params
//I have a function getHostWebUrl() - which is not included.
// Some Ideas Here: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
var hostWebUrl = getHostWebUrl();
//Using the hostWebContext as an AppContextSite
hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);
//Create List Code
var listCreation = new SP.ListCreationInformation();
listCreation.set_title(title);
listCreation.set_templateType(templateType);
listCreation.set_url(url);
//must use the hostWebContext to get the list in that site
var lists = hostWebContext.get_web().get_lists();
var list = lists.add(listCreation);
list.set_hidden(hidden);
list.set_onQuickLaunch(false);
list.update();
//Always use the context of the app web to do the work or load and executing
clientContext.load(list);
clientContext.executeQueryAsync(function() {
console.log("Created List : \"" + title + "\"");
}, function(sender, args) {
console.log("Failed to create list : " + title);
console.log("Reason : " + args.get_message() + "");
});
}
// parameters : List Name, List Url, Template Type (can be ListTemplateType or Template ID, Hidden)
CreateList("My Test List", "Lists/MyTestList", SP.ListTemplateType.genericList, false);
CreateList("My Test List", "Lists/MyTestList", 171, false);
Show Comments