2

I want to get all list items from all lists in SharePoint 2010 with JavaScript. I always get the error:

SCRIPT5022: 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.

Here is my code:

ExecuteOrDelayUntilScriptLoaded(receivedAllLists, "SP.js");

function receivedAllLists() {
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();
    clientContext.load(collList);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onreceivedAllListsSucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onreceivedAllListsSucceeded() {
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    var listInfo = '';

    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        oList = oWebsite.get_lists().getByTitle(listTitle); 
        var splistquery = new SP.CamlQuery();
        splistitems = oList.getItems(splistquery);
        clientContext.load(splistitems, 'Include(Id)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onrecieveAllListItemsSucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQueryFailed(sender, args) {
    alert('Fail');
}


function onrecieveAllListItemsSucceeded() {


    var listItemInfo = '';
    var listItemEnumerator = splistitems.getEnumerator();

    while (listItemEnumerator.moveNext()) {
    var currentlistitem = listItemEnumerator.get_current();

    }

}
2
  • For which execute query you are getting this error?
    – Unnie
    Commented Jun 14, 2015 at 21:24
  • I get the error in the second execute query.
    – Paola
    Commented Jun 14, 2015 at 22:01

1 Answer 1

0

You should not call executeQueryAsync method in loop as it will generate multiple async calls and execution will get messed up in deciding which onrecieveAllListItemsSucceeded method to execute. That is reason most of the time we 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.
Check this link for similar issue resolved https://sharepoint.stackexchange.com/a/146869/40602

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.