I trying to make simple sharepoint app on javascript. I create list, named "test" and add some records where. After starting my app i get error: "The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested". It raised in success function. Here is code:
'use strict';
var web;
var hostweburl;
var appweburl;
var collListItem;
function sharePointReady() {
hostweburl =
decodeURIComponent(
getQueryStringParameter('SPHostUrl')
);
appweburl =
decodeURIComponent(
getQueryStringParameter('SPAppWebUrl')
);
var scriptbase = hostweburl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.js', function () {
$.getScript(scriptbase + 'SP.Runtime.js', function () {
$.getScript(scriptbase + 'SP.RequestExecutor.js', printAllListNamesFromHostWeb).fail(function () { alert("fail") });
}).fail(function () { alert("fail") });
}).fail(function () { alert("fail") });
//ExecuteOrDelayUntilScriptLoaded(printAllListNamesFromHostWeb, "SP.js");
//printAllListNamesFromHostWeb();
}
function getQueryStringParameter(param) {
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] == param) {
return singleParam[1];
}
}
}
function printAllListNamesFromHostWeb() {
var context;
var factory;
var appContextSite;
context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);
web = appContextSite.get_web();
var oList = web.get_lists().getByTitle("testList");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>10</RowLimit></View>');
collListItem = oList.getItems(camlQuery);
context.load(collListItem, 'Include(Id, Title)');
context.executeQueryAsync(
//Function.createDelegate(this, success),
//Function.createDelegate(this, errorHandler)
success, errorHandler
);
}
function success(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title');
}
alert(listItemInfo.toString());
}
function errorHandler(sender, args) {
alert('Fail: ' + args.get_message() + '\n' + args.get_stackTrace());
}
sharePointReady();