0

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 apply theme. but the url isn't correct.

Follow 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(Url, ServerRelativeUrl,ParentWeb)');
   context.executeQueryAsync(
    function () {  
         subWebs = subWebs.getEnumerator();
         while(subWebs.moveNext()){
            var subWeb = subWebs.get_current();
            var testUrl = subWeb.get_url();
            alert(testUrl);
         }
    },
    function (sender, args) {
       console.log(args.get_message());
    }
   );   
}

Follow image from Project that have debug: debug image failed url

Correct: The expected is : https://domain.sharepoint.com/sites/devsite/sub1

Incorrect: The result is : https://domain-XX1634579846354987.sharepoint.com/sites/devsite/sub1

And not apply theme for all subsite.

note: this method is recursive.

Some idea about how to resolve this question?

  • Probably domain.sharepoint.com is a DNS alias for the load balancing server. – Yvan Dec 17 '14 at 19:28
  • Its office 365. Elaborate your answer on how to achieve this in SharePoint online. – Amal Hashim Dec 17 '14 at 19:30
  • OK sorry I tought sharepoint.com was a server name in his network. In this case domain-xxxxx must be the real server name of his sharepoint site. SharePoint is returning the correct result. – Yvan Dec 17 '14 at 19:33
1

What you're seeing is correct url (a subsite). From your debug image, it looks like you have apps. Apps in SP 2013 get their own site where the naming convention is ' https://domain-XXXXXXXXXXXXXXXXXX.sharepoint.com/sites/site1/subsite1'

To exclude the app sites (example):

 while (webEnumerator.moveNext()) {
      var newweb = webEnumerator.get_current();
      if (newweb.get_webTemplate() == 'APP')
         continue;

      var obj = {
           title: newweb.get_title(),
           url: newweb.get_serverRelativeUrl(),
      };
      window.console && console.log('Title: ' + obj.title + ' Url: ' + obj.url);
 }

Change your code in your debug image as:

while(subsitesweb.moveNext()) {
    var web = subsitesweb.get_current();
    var webTitle = web.get_title();
    if(webTitle && web.get_webTemplate() !== 'APP') {
       //web.set ...
       web.update();
       web.applyTheme(ColorPalleteURL, fontscheme, background, true);
    }
}
  • but this subsite1 not is app and exclude for apply branding, and not do as expected. Returned full url incorrect, i don't need serverrelativeurl, i cant use full url to create new context site and apply, but as the url is incorrect, throw error why url not found. i will open ticket for Microsoft reporting this error. – b1c10 Dec 19 '14 at 13:24
  • Apps in SP are essentially a subsite, so you'd need to exclude them when enumerating for subsites. See the updated answer. Also, the code you posted doesn't match your debug image. – Aveenav Dec 19 '14 at 16:25
0

I tried below on my online setup and it worked properly

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, Url, ServerRelativeUrl,ParentWeb)');
context.executeQueryAsync(
    function () {  
        for (var i = 0; i < subWebs.get_count(); i++) {
            var subWeb = subWebs.itemAt(i);
            console.log(subWeb.get_url());
        }
    },
    function (sender, args) {
        console.log(args.get_message());
    }
);
  • 1
    again Url not result as expected :/ when get sub site level 2 the url is incorrect. – b1c10 Dec 17 '14 at 16:22
  • example: rootweb -> subweb work fine – b1c10 Dec 17 '14 at 16:22
  • rootweb -> subweb -> subweb fail – b1c10 Dec 17 '14 at 16:23

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.