Skip to content

Instantly share code, notes, and snippets.

@vgrem
Created April 11, 2015 14:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/b1e5273139607363bef9 to your computer and use it in GitHub Desktop.
Save vgrem/b1e5273139607363bef9 to your computer and use it in GitHub Desktop.
Enumerate webs of site collection using SharePoint JSOM
function enumWebs(propertiesToRetrieve, success,error)
{
var ctx = SP.ClientContext.get_current();
var rootWeb = ctx.get_site().get_rootWeb();
var result = [];
var level = 0;
ctx.load(rootWeb,propertiesToRetrieve);
result.push(rootWeb);
var colPropertiesToRetrieve = String.format('Include({0})',propertiesToRetrieve.join(','));
var enumWebsInner = function(web,result,success,error)
{
level++;
var ctx = web.get_context();
var webs = web.get_webs();
ctx.load(webs,colPropertiesToRetrieve);
ctx.executeQueryAsync(
function(){
for(var i = 0; i < webs.get_count();i++){
var web = webs.getItemAtIndex(i);
result.push(web);
enumWebsInner(web,result,success,error);
}
level--;
if (level == 0 && success)
success(result);
},
error);
};
enumWebsInner(rootWeb,result,success,error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment