Tuesday, 11 November 2014

SharePoint 2013 getting all webs in a given site collection including root web using JavaScript Object Model

Using JSOM I'am getting the collection of all the webs(sub-sites) including the root web of a given site collection and display there title.

Below is the code :

function getWebs()
{
   var context = SP.ClientContext.get_current();
   var web = context.get_web(); 
   var site = context.get_site();
   var rootWeb = site.get_rootWeb();
   var subWebs = rootWeb.get_webs();

   context.load(subWebs, 'Include(Title, ServerRelativeUrl,ParentWeb)');
   context.executeQueryAsync(
    function () {  
         for (var i = 0; i < subWebs.get_count(); i++) {
            var subWeb = subWebs.itemAt(i);
            var parentWeb = subWeb.get_parentWeb();  //parent web ==  root web
            console.log(subWeb.get_title());
          }
    },
    function (sender, args) {
       console.log(args.get_message());
    }
   );   
}

No comments:

Post a Comment