You can either continue this article or use the below code in your own way. This code is written in Apps.js file.
Below are the sample JavaScript codes for lists operations:
var hostWebUrl;
var appWebUrl;
var ctx ;
var appCtxSite;
var web;
$(document).ready(function () {
hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
ctx = new SP.ClientContext(appWebUrl);
appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
web = appCtxSite.get_web();
getAllLists();
});
// function to delete a list
function deleteList() {
var list = web.get_lists().getByTitle('My Contacts');
list.deleteObject();
ctx.executeQueryAsync(
Function.createDelegate(this, function () { alert("List deleted successfully");}),
Function.createDelegate(this, this.onFail));
}
//function to update an existing list
function updateList() {
var list = web.get_lists().getByTitle('My Contacts');
list.set_description('My Personal Contacts');
list.update();
ctx.load(list);
ctx.executeQueryAsync(Function.createDelegate(this, function () {
alert("Title: " + list.get_title() + " , Description: " + list.get_description());
}), Function.createDelegate(this, this.onFail));
}
// function to create new list in the host web
function createList()
{
var listInfo = new SP.ListCreationInformation();
listInfo.set_title('My Contacts');
listInfo.set_templateType(SP.ListTemplateType.contacts);
this.list = web.get_lists().add(listInfo);
ctx.load(list);
ctx.executeQueryAsync(
Function.createDelegate(this, function () {
alert(this.list.get_title() + " new list created!");
}),
Function.createDelegate(this, this.onFail));
}
// function to get all List names in the host web
function getAllLists() {
this.listColl = web.get_lists();
ctx.load(this.listColl);
ctx.executeQueryAsync(
Function.createDelegate(this, this.onGetAllListsSucceed),
Function.createDelegate(this, this.onFail));
}
// success method of getAllLists function
function onGetAllListsSucceed() {
//var list = '';
var listEnumerator = this.listColl.getEnumerator();
while(listEnumerator.moveNext()){
var list = listEnumerator.get_current();
document.getElementById("message").innerHTML = document.getElementById("message").innerHTML + list.get_title() + "</br>";
}
}
// generic Fail method
function onFail()
{
alert("Failed!" + arguments[1].get_message());
}
// function to get the specific list properties
function getSpecificListProperties() {
this.listColl = web.get_lists();
ctx.load(listColl, 'Include(Title, Id)');
ctx.executeQueryAsync(
Function.createDelegate(this, this.onGetSelectedListProperties),
Function.createDelegate(this, this.onFail));
}
// success method of getSpecificListProperties function
function onGetSelectedListProperties()
{
var listEnumerator = this.listColl.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
document.getElementById("message").innerHTML = document.getElementById("message").innerHTML +
list.get_title() + " -- " + list.get_id().toString() + "</br>";
}
}
// function to manage the query string parameters and return SPHostURL or SPAppWebUrl as per the request.
function manageQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve) {
return singleParam[1];
}
}
}
