Hi, the below is my code.
Instead of For loop for projectList, I just called them separately as my lists have different names. They both have the same column names.
Yes, all the municipality names are being returned, not just the one I specified. In this case “Surrey”
var handlers = {};
handlers.init = function(data, logger) {
return true;
}
handlers.requestInit = function(query, logger) {
return true;
}
handlers.requestSuccess = function(data, logger) {
var dfd = $.Deferred();
var projectList = data.items;
data.items = [];
var deferreds = [];
var query = new SP.CamlQuery('<View><ViewFields><FieldRef Name="Area"/><FieldRef Name="Municipality"/><FieldRef Name="Population"/> <FieldRef Name="Date"/> </ViewFields><Query><Where><Eq><FieldRef Name ="Municipality" /><Value Type="text">Surrey</Value></Eq></Where></Query></View>');
var d = request(query, projectList[0], 'Municipal Urban Development', ['Municipality', 'Area', 'Population', 'Date'], data, logger);
deferreds.push(d);
var e = request(query, projectList[1], 'Urban Centres Development', ['Municipality','Area', 'Population', 'Date'], data, logger);
deferreds.push(e);
/*for (var project in projectList){
//logger.debug("lists", projectList[project]);
var d = request(query, projectList[project], 'Urban Centres Development', ['Population', 'Year'], data);
deferreds.push(d);
}*/
logger.debug('All requests fired.');
$.when.apply(null, deferreds).done(function() {
console.log('All requests have been resolved.');
dfd.resolve();
});
return dfd.promise();
}
handlers.requestError = function(error, logger) {
return $.Deferred().reject(error);
}
handlers.aggregationSuccess = function(data, logger) {
return true;
}
handlers.aggregationError = function(error, logger) {
return $.Deferred().reject(error);
}
handlers.finish = function(data, logger) {
logger.debug('Data is processed: ', data);
return true;
}
function request(query, project, listName, fields, data, logger) {
var dfd = $.Deferred();
var ctx = new SP.ClientContext(project['URL']);
var list = ctx.get_web().get_lists().getByTitle(listName);
var itemCollection = list.getItems(query);
ctx.load(itemCollection, 'Include(' + fields.join(',') + ')');
ctx.executeQueryAsync(function () {
var itemCollectionEnumerator = itemCollection.getEnumerator();
while (itemCollectionEnumerator.moveNext()) {
var listItem = itemCollectionEnumerator.get_current();
var item = {};
$.each(fields, function () {
try {
var value = listItem.get_item(this);
item[this] = value;
}
catch (e) { }
});
item['Project'] = project['Title'];
data.items.push(item);
}
}
console.log('Request to ' + project['URL'] + ' has been resolved.');
dfd.resolve();
}, function(caller, args){
console.log('Error: ' + args.get_message());
});
return dfd.promise();
}