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>' +
'</Leq></Where></Query><RowLimit>50</RowLimit></View>');
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();
}
alert(listItemInfo.toString());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}