Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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...
}
share|improve this answer
    
btw... I've created a library that does all of this creates reusable wrapper around CSOM, check it out here: github.com/monzerhijazi/SpUtil –  onzur Jun 17 at 20:12
    
Many thanks, you have got sharp eye :-) Finally it work. And I will definitely check your library. –  mimo Jun 17 at 20:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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