Iam trying to create a sitemap for sharepoint sites to get a hierarchial structure of all the subsites within a site. but Iam only able to get sites of that site

eg: -Lets say i have created below sites -


So if i am putting the script on Site named Boy i am only able to fetch till Boy1and NOT Boy2...I cannot use any third party tool webpart..Iam using script from below link...

Fetch list of sites in site collection into jQuery

  • It looks like that script doesn't have a recursion point that would need to be called on the spweb.webs equivalent of client side... I don't really know sharepoint client side yet but I believe thats the problem.
    – BlueBird
    Apr 1, 2013 at 12:09
  • for(var i = 0; i < this.webs.get_count(); i++){ }
    – Rameez
    Apr 1, 2013 at 12:54
  • above is the code which is used for recursion..but only giving output to only one level of sites
    – Rameez
    Apr 1, 2013 at 12:55

2 Answers 2


I think that the best approach is to use lazy load functionality. Fist of all, you can use solution from link that you provided to build fist level of the webs structure. In the onWebsLoaded method you can create html element that expands next menu items level. On this element you can set onclick event handler that retrieves server relative url of the web that is associated with this element, retrive the web by this url and load sub webs as describet in the post. Then create a new level of menu. You can repeat this steps for the next level of items.

You can store server relative url of the web in the attribute of html element. In the onclick event of this element you can retrieve this url and use this code

var url = $(this).attr("serverRelativeUrl");
var clientContext = new SP.ClientContext(url);
this.subwebs = clientContext.get_web().get_webs();

clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed); 

then you can create and add new links based on response.

  • Hi Alexander Iam not aware with concept of lazy loading.But my question how would i be able to go to the second level of sites.Because this.webs = clientContext.get_web().get_webs(); this statement is only giving me subsites till level 1 and not level 2 as i mentioned in my 1st post its giving till Boy1 ONLY. Could you please provide a code to iterate to the second level.That would really help
    – Rameez
    Apr 2, 2013 at 6:50

Hi I got the answer to my Question it can be done withe below code -

var clientContext; function onQueryFailed(sender, args) { alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); }

/* recursive call for web */ function recursiveAll(cweb) { alert(cweb.get_title()); var subwebs = cweb.get_webs(); clientContext.load(subwebs); clientContext.executeQueryAsync(function(sender, args) { var webEnum = subwebs.getEnumerator(); while(webEnum.moveNext()){ var web = webEnum.get_current(); alert(web.get_title()); //recursive call for current web. recursiveAll(web); } }, function(){alert('err');}); } function onWebsLoaded(sender, args) { for (var i = 0; i < this.webs.get_count(); i++) { this.subwebs = this.webs.itemAt(i); recursiveAll(this.subwebs) } } function loadWebs() { clientContext = new SP.ClientContext.get_current(); this.webs = clientContext.get_web().get_webs(); clientContext.load(this.webs); clientContext.executeQueryAsync(Function.createDelegate(this, this.onWebsLoaded), Function.createDelegate(this, this.onQueryFailed)); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.