I'm about to query multiple lists using JavaScript (JSOM, SharePoint 2010). It's working when querying one list in a time, but that's annoying when I'm having lots of lists. Working example:
function createLists() {
getListData1();
getListData2();
}
function getListData1() {
var context = new SP.ClientContext(url);
var camlQuery = new SP.CamlQuery();
[...]
context.load(listItems);
context.executeQueryAsync(
Function.createDelegate(this, this.onQuerySuccess1),
Function.createDelegate(this, this.onQueryFailed)
);
}
function getListData2() {
[...]
context.executeQueryAsync(
Function.createDelegate(this, this.onQuerySuccess2),
Function.createDelegate(this, this.onQueryFailed)
);
}
But what I want to do is to reuse the function extractData, like this:
function createLists() {
getListData(url, List1);
getListData(url, List2);
}
function getListData(siteUrl, listName) {
var context = new SP.ClientContext(siteUrl);
var camlQuery = new SP.CamlQuery();
var list = context.get_web().get_lists().getByTitle(listName);
[...]
context.load(listItems);
context.executeQueryAsync(
Function.createDelegate(this, this.onQuerySuccess),
Function.createDelegate(this, this.onQueryFailed)
);
}
Unfortunately that seems to create some conflict, probably has to do with the asynchronous function? Is it possible to reuse an asynchronous call like this or do I need to create individual functions for each and every query?
executeQueryAsync
? This is one of the most obvious strengths of JSOM.