Is here any SharePoint expert, who can help me find out, why I am getting error on lists queries?
I am trying to fetch data from multiple list asynchronously, but I am getting following 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.
My code looks like this:
function loadAllData() {
loadTeam();
loadTasks();
loadTaskTypes();
}
Where load functions looks like:
function loadTeam() {
retrieveListItemsInclude(teamListProperties);
}
function loadTasks() {
retrieveListItemsInclude(tasksListProperties);
}
...
And retrieveListItemsInclude
function looks like:
function retrieveListItemsInclude(listProperties) {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle(listProperties.listName);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>1000</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);
var includeListProperties = buildInclude(listProperties);
clientContext.load(collListItem, 'Include(Title,ID)');
switch(listProperties.listName) {
case 'Task list':
clientContext.executeQueryAsync(function(){ onTaskQuerySucceeded(listProperties); }, failCallback);
break;
case 'Task types list':
clientContext.executeQueryAsync(function(){ onTaskTypesQuerySucceeded(listProperties); }, failCallback);
break;
...
case 'Team list':
clientContext.executeQueryAsync(function(){ onTeamQuerySucceeded(listProperties); }, failCallback);
break;
}
}
And finally success callbacks look like:
function onTasksQuerySucceeded(listProperties) {
var tasks = [];
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var task = {};
task.id = oListItem.get_item('ID');
task.title = oListItem.get_item('Title');
tasks.push(task);
}
console.log("Tasks loaded.");
}
When running loadAllData()
function, I get above error, when calling collListItem.getEnumerator()
in all of success callbacks, but the last.
This means, if I call loadTeam(); loadTasks(); loadTaskTypes();
in loadAllData()
, I get error in first two success callbacks, but loadTaskTypes()
will execute properly.
Why is this happening? In retrieveListItemsInclude(listProperties)
, I create local clientContext
for every request to SharePoint list... why are the calls interfering?
When I used jQuery promises to chain calls loadTeam(); loadTasks(); loadTaskTypes();
one after another, all data loaded without error.
I appreciate any help.