Skip to content

Instantly share code, notes, and snippets.

@vgrem vgrem/getAllWebs.js
Created Feb 2, 2015

Embed
What would you like to do?
Retrieve all webs using SharePoint JSOM
function getAllWebs(success,error)
{
var ctx = SP.ClientContext.get_current();
var web = ctx.get_site().get_rootWeb();
var result = [];
var level = 0;
var getAllWebsInner = function(web,result,success,error)
{
level++;
var ctx = web.get_context();
var webs = web.get_webs();
ctx.load(webs,'Include(Title,Webs)');
ctx.executeQueryAsync(
function(){
for(var i = 0; i < webs.get_count();i++){
var web = webs.getItemAtIndex(i);
result.push(web);
if(web.get_webs().get_count() > 0) {
getAllWebsInner(web,result,success,error);
}
}
level--;
if (level == 0 && success)
success(result);
},
error);
};
getAllWebsInner(web,result,success,error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.