1

I want to make a function to get data from SP list thru jsom, when i run the function with multiple items I get the error, with only one item it does work correct.

Error:

Uncaught Error: The collection was not initialized. The initialization is not requested or the application is not running. Possibly the initialisation must be made explicit.

My code: (error comes up when run go and fails on this.collListItem.getEnumerator();)

function go() {
  for (var i = 0; i < 5; i++) {
    getEmployee2(i);            
  }
}

function getData(number) {
  var clientContext = new SP.ClientContext(theSite);
  var oList = clientContext.get_web().get_lists().getById(emplist);

  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Number"/><Value Type="Text">' + number + '</Value></Eq></Where></Query></View>');
  this.collListItem = oList.getItems(camlQuery);

  clientContext.load(collListItem);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() 
{
  var listItemInfo = '';
  var listItemEnumerator = this.collListItem.getEnumerator();

  while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    console.log('emp' + oListItem.get_id());
  }
}

function onQueryFailed() {
  alert('fail');
}

How can I solve this? I doesn't matter if it's runs sync or async. I think collListItem is used multiple times giving the error?

2 Answers 2

1

Your problem is scope, solving it by creating globals isn't considered good practice.

So pass the payload to your functions:

with: processItems.bind(request)

function getItems(listName,siteUrl){
    var request={};
    request.listName=listName;
    request.siteUrl=siteUrl || _spPageContextInfo.siteAbsoluteUrl;
    var ctx = new SP.ClientContext(request.siteUrl);
    var list = ctx.get_web().get_lists().getByTitle(request.listName);
    request.query=SP.CamlQuery.createAllItemsQuery();
    request.itemCollection=list.getItems(request.query);
    ctx.load(request.itemCollection);
    ctx.executeQueryAsync(  processItems.bind(request)  ,failure);
}
function processItems() {
    var received=this;//received 'request' from Caller
    received.items=[];
    var listEnumerator = received.itemCollection.getEnumerator();
    while (listEnumerator.moveNext()) {
        var listitem = listEnumerator.get_current();
        received.items.push(listitem.get_id());
    }
    console.info(received.listName,'has',received.items.length,'items IDs:',received.items);
}
function failure(sender, args){
    console.error(args.get_message());
}
console.clear();
for(var cycle=1;cycle<4;cycle++){// call multiple times to show ASYNC result in the console
    console.log('Calling 3 lists cycle:',cycle);
    getItems('Tasks');
    getItems('Documents');
    getItems('Images');
}

You could have bound just the itemCollection itself, but I elaborated a bit to show how to pass more data.

For (oldschool) CreateDelegate and (since ES-5) bind() differences see: Differing ways to make executeQueryAsync calls - Function.createDelegate (is old IE8 code)

Run this and you will see the asynchronous responses:
(Tasks was requested first, but Documents was received first, etc)

J1 J5 iBind

0

Please try to declare "collListItem" globally.

Please refer below updated code and let me know if face any problem.

function go() {
  for (var i = 0; i < 5; i++) {
    getEmployee2(i);            
  }
}

var collListItem = "";
function getData(number) {
  var clientContext = new SP.ClientContext(theSite);
  var oList = clientContext.get_web().get_lists().getById(emplist);

  var camlQuery = new SP.CamlQuery();
  camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Number"/><Value Type="Text">' + number + '</Value></Eq></Where></Query></View>');
  collListItem = oList.getItems(camlQuery);

  clientContext.load(collListItem);
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() 
{
  var listItemInfo = '';
  var listItemEnumerator = collListItem.getEnumerator();

  while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    console.log('emp' + oListItem.get_id());
  }
}

function onQueryFailed() {
  alert('fail');
}

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.