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() {


Where load functions looks like:

function loadTeam() {
function loadTasks() {

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();

    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);
        case 'Task types list':
            clientContext.executeQueryAsync(function(){ onTaskTypesQuerySucceeded(listProperties); }, failCallback);
        case 'Team list':
            clientContext.executeQueryAsync(function(){ onTeamQuerySucceeded(listProperties); }, failCallback);

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');


    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.

1 Answer 1


Very close, but your problem is still in JavaScript scopes :) ... specifically:

this.collListItem = oList.getItems(camlQuery);

When the success call back comes through, the references have gotten messed up because the second function call overrode the colListItem (which is on the global scope) from first call.

Since you're already using listProperties as a unique variable you can utilize that further. Change the three lines to the following:

listProperties.colListItem = oList.getItems(camlQuery)
var includeListProperties = buildInclude(listProperties);
clientContext.load(listProperties.collListItem, 'Include(Title,ID)');

Inside your success call back:

function onTasksQuerySucceeded(listProperties) {
    var tasks = [];
    var listItemEnumerator = listProperties.collListItem.getEnumerator();
    //rest of your code...
  • btw... I've created a library that does all of this creates reusable wrapper around CSOM, check it out here: github.com/monzerhijazi/SpUtil
    – mhijazi
    Jun 17, 2014 at 20:12
  • Many thanks, you have got sharp eye :-) Finally it work. And I will definitely check your library.
    – mimo
    Jun 17, 2014 at 20:25
  • @onzur Would the library also allow me to take a returned value from one list and query another list?
    – Si8
    May 24, 2016 at 15:29
  • @Si8 Yes, you have to get the first item and in the success callback get the item from the other list. var list1 = new SpListUtil("list1_title") var list2 = new SpListUtil("list2_title") list.getItemById(1, { success: function(item1){ list2.getItemById(item1.field, { success: function(item2) { console.log(item2.field) } } } });
    – mhijazi
    May 24, 2016 at 16:27
  • 1
    @Si8 can you paste your code?
    – mhijazi
    May 24, 2016 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.