1

Using JSOM I am trying to get 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 snippet. But it throws error "get_allWebs()"property is not supported.

        var context = new SP.ClientContext.get_current();
        var web = context.get_web(); 
        var siteColl = context.get_site();
         var webSubSites = siteColl.get_allWebs();

          context.load(webSubSites, 'Include(Title, ServerRelativeUrl)');

         context.executeQueryAsync(
         Function.createDelegate(this, function (sender, args) {  

         for (var x = 0; x < webSubSites.get_count(); x++) {
             var subWeb = webSubSites.itemAt(x);                 
            alert(subWeb.get_title());
          }

        }),
         Function.createDelegate(this, function (sender, args) {
             console.log('Request failed. ' + args.get_message() + '\n' +   args.get_stackTrace());
         }));

I am able to get all the subwebs\subsites, but not rootweb.

1

3 Answers 3

3

The error Property is not supported occurs since SP.Web object does not contain AllWebs property. From another hand SP.Web object exposes SP.Web.webs property returns all Web sites immediately beneath the Web site, excluding children of those Web sites.

You could use SP.Web.parentWeb property in order to return parent web that corresponds to root web for 1st level sub site, the following example demonstrates how it could be achieved:

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

Here's a script that would get you the root web's title. To get the sub-sub sites, you'd need a recursive function.

var context = new SP.ClientContext.get_current();
// context = new SP.ClientContext(siteRelativeUrl);
var web = context.get_web(); 
var siteColl = context.get_site();
var rootWeb = siteColl.get_rootWeb();
var subWebCollection = web.getSubwebsForCurrentUser(null);

context.load(rootWeb);
context.executeQueryAsync(
  function(){
     window.console && console.log(rootWeb.get_title());

  },
  function (sender, args) {
     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }
);

Note: web.getSubwebsForCurrentUser(null); would only return the sites you have access to. Else some user may get access denied when trying to read web properties.

Hope this helps.

1
  • Thanks a lot Aveenav. Your post helped me fetch the subsites for a user with read-only permission
    – Sagar
    Commented Jan 10, 2019 at 10:02
1

Wouldn't you need to do something like this:

 var rootWeb = context.get_rootWeb();

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.