I've pulled the following example of a REST Query from the SharePoint 2010 Documentation. It's working well except when I try to run multiple queries at once by renaming the functions:

retrieveListItems() and onQuerySucceeded(sender, args) to retrieveListItems2() and onQuerySucceeded2(sender, args) respectively.

I'm also renaming Function.createDelegate(this, this.onQuerySucceeded to Function.createDelegate(this, this.onQuerySucceeded2 within retrieveListItems2().

If I chain these calls by adding, say retrieveListItems3() at the end of the onQuerySucceeded2(sender, args) function I do not get a conflict.

However this is starting to get unwieldy with one page calling over a dozen lists and having to track each of the differently named functions. It's also starting to get very slow (and unreliable) with the next chained call requiring the event to succeed.

Is it possible to wrap these functions in another method or something so I can use them as a framework of sorts, or do I have to keep renaming them? More over how can I run multiple simultaneous calls without getting the following error: Uncaught 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.

function retrieveListItems() {

    var clientContext = new SP.ClientContext('/sites/MySiteCollection');
    var oList = clientContext.get_web().get_lists().getByTitle('My List');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Leq>' + 
        '<FieldRef Name=\'ID\'/><Value Type=\'Number\'>100</Value>' + 

    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(Id, DisplayName)');

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


function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nDisplay name: ' + oListItem.get_displayName();


function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

Look into jQuery promises if you're not familiair with them: http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/

It can be somewhat of a hard concept to learn, but it's very powerfull if you understand it.

With jQuery promises you can run multiple asynchronous calls at the same time and use $.when() to execute code when all the required calls are done. https://api.jquery.com/jquery.when/

  • Thanks for this, I've read a bit about JQuery Promises but have steered clear. Look as though I've got some learning to do :) – sansSpoon Jan 13 '16 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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