UPDATE changed code, when it runs it prints out the 3 list names as expected but the While loop always shows the items from the last list, it needs to show items from all the lists.
From the 3 lists I have (one in every subsite) the code outputs the last list items 3 times. It should output List A items, List B items etc
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
var myCtx = SP.ClientContext.get_current();
var root = myCtx.get_site().get_rootWeb(); // may need to do this if in a sub site
var webs = root.get_webs(); // fetch a reference to all the sub webs
var query = new SP.CamlQuery();
// only need to fetch the first item returned from each Project list
//query.set_viewXml('<View Scope="RecursiveAll"><Query></Query><RowLimit>1</RowLimit></View>');
query.set_viewXml('<View><RowLimit>100</RowLimit></View>');
var queryItemCollections = [];
// make sure to load the lists data so we can check
// if the "Projects" list exists in each site
myCtx.load(webs, "Include(Id, Lists)");
myCtx.executeQueryAsync(function () {
// push new SP.ListItemCollections into an array and tell the ctx to fetch them
//console.log(webs.get_data().length);
for (var i = 0; i < webs.get_data().length; i++) {
var web = webs.get_data()[i], itms;
if (checkForProjectsList(web)) {
itms = webs.get_data()[i].get_lists()
.getByTitle("TeamRequests").getItems(query);
queryItemCollections.push(itms);
myCtx.load(itms);
}
}
myCtx.executeQueryAsync(function () {
if (queryItemCollections.length == 0) {
console.log("collection empty");
}
//console.log('len '+queryItemCollections.length);
var count=0;
queryItemCollections.forEach(function (item) {
// do work with individual list items here
var listItemEnumerator = itms.getEnumerator();
var listItemInfo = '';
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
listItemInfo = oListItem.get_item('_ProjectRef') + '\n+++++++++\nID: ' + oListItem.get_id() +
'\nTitle: ' + oListItem.get_item('Title')+'\n-------';
console.log( listItemInfo);
} // end while
console.log(item.getItemAtIndex(0).get_item("_ProjectRef")); // this show correct lists
count++;
});
}, genericFailHandler);
}, genericFailHandler);
function checkForProjectsList(web) {
var lists = web.get_lists().get_data();
for (var j = 0; j < lists.length; j++) {
var list = lists[j];
if (list.get_title() == "TeamRequests") {
return true;
}
}
return false;
}
function genericFailHandler(sender, args) {
console.log(args.get_message());
}
}, "SP.js");