I'm getting all the subwebs from a site in my app in SharePoint with:
var getW = getWebs($q)
.then(function (results) {
console.log(results); // Object with results from the first execute.
});
function getWebs($q) {
var deferred = $q.defer();
//App context etc..
web = appContextSite.get_web();
subWebs = web.getSubwebsForCurrentUser(null);
context.load(subWebs, 'Include(Url, Created, Title, Lists)');
context.executeQueryAsync(Function.createDelegate(this, function () {
enumSubWebs = subWebs.getEnumerator();
var arraySubWebs = [];
var list = [];
while (enumSubWebs.moveNext()) {
var subWeb = enumSubWebs.get_current(),
subWebUrl = subWeb.get_url();
var _list = subWeb.get_lists().getByTitle('Custom List');
list.push({
'listItem': _list.getItemById(1),
'webTitle': subWeb.get_title()
});
context.load(list[list.length - 1].listItem);
promises.push(list);
}
context.executeQueryAsync(Function.createDelegate(this, function () {
for (var i = 0; i < list.length; i++) {
console.log(list[i].listItem.get_fieldValue()['Title']);
}
// Not sure what to put here, because the promise is returned to my .then above before it enters here.
}));
Q.allSettled(promises).then(function () {
deferred.resolve(list);
});
}));
return deferred.promise;
};
Then I would like to get a list item by ID (in 'Custom list') from each web.
I'm not sure where I'm doing anything wrong, but the the webs (incl list array) is returned, but listItem doesn't seems to be executed. How should I use the promise to return everything after the last executeQuery?