3
function populateList(){
    var _clientContext;
    var _web;
    _clientContext = new SP.ClientContext.get_current();
    _web = _clientContext.get_web();
    var listTitle = "Data1";
    var list = _web.get_lists().getByTitle(listTitle);
    var myquery = new SP.CamlQuery();
    myquery.set_viewXml('valid query');
    var allItems = list.getItems(myquery);
    _clientContext.load(allItems);  
    _clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded()), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var listEnumerator = this.allItems.getEnumerator(); //this craps out
}

I get the error

"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."

5
  • what is the query you are using? Oct 31, 2014 at 12:08
  • 3
    Have you tried allItems.getEnumerator(); instead of this.allItems? It might be an issue with the scopes.
    – wjervis
    Oct 31, 2014 at 12:14
  • I put a setTimeout around my allItems = list.getItems(myquery); and _clientContext.load(allItems); and it seems to work, now I just need to figure out if I can incorporate a callback on those
    – John
    Oct 31, 2014 at 15:03
  • John, you absolutely, one-hundred percent, do not need to use setTimeout to make this work...
    – John-M
    Oct 31, 2014 at 19:58
  • John-o you're right. I'm still struggling with this problem. I think there is an issue with the sequence of it loading. I have the call to populateLists inside of an onclick event: <button ID="retrieveList" onclick="ExecuteOrDelayUntilScriptLoaded(populateList, 'sp.js');return false">Retrieve List</button>
    – John
    Nov 6, 2014 at 12:33

2 Answers 2

5

The problem lies with object scope. Try below

var allItems;
function populateList(){
    var _clientContext;
    var _web;
    _clientContext = new SP.ClientContext.get_current();
    _web = _clientContext.get_web();
    var listTitle = "Data1";
    var list = _web.get_lists().getByTitle(listTitle);
    var myquery = new SP.CamlQuery();
    myquery.set_viewXml('valid query');
    allItems = list.getItems(myquery);
    _clientContext.load(allItems);  
    _clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var listEnumerator = allItems.getEnumerator(); //this craps out
}
0

If you do not want to declare allItems variable outside method then the other way would be to declare allItems inside block but with a 'this' keyword

function populateList(){
    var _clientContext;
    var _web;
    _clientContext = new SP.ClientContext.get_current();
    _web = _clientContext.get_web();
    var listTitle = "Data1";
    var list = _web.get_lists().getByTitle(listTitle);
    var myquery = new SP.CamlQuery();
    myquery.set_viewXml('valid query');
    this.allItems = list.getItems(myquery);
    _clientContext.load(this.allItems);  
    _clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var listEnumerator = allItems.getEnumerator();
}

Your Answer

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

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