7

I have used the following code from Most efficient way to get all sub sites under a site collection/sub site using 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);    
}   

getAllWebs(
    function(allwebs){
       for(var i = 0; i < allwebs.length;i++){
         console.log(allwebs[i].get_title());   
       }
    },
    function(sendera,args){
      console.log(args.get_message());
});

but this one is only returning all subsites' titles, except for the rootweb. I have tried a few ways but not successful. So what should I need to do to access to the rootweb as well? Thank you.

9

You could consider the following modified example for enumerating all sites within site collection:

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);    
}

Gist: enumWebs.js

Key Points:

  • propertiesToRetrieve parameter is used for specifying what Web properties to retriece

Usage

Print web sites title:

enumWebs(['Title'],
   function(webs){
       for(var i = 0; i < webs.length;i++){
          console.log(webs[i].get_title());                               
       }
   },
   function(sender,args){
      console.log(args.get_message());
   });  
2

You should request for the root web

var web = ctx.get_site().get_rootWeb();
ctx.load(web,'Include(Title)');
ctx.executeQueryAsync(...);

SharePoint 2013 getting all webs in a given site collection including root web using JSOM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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