user1666888
user1666888

Reputation: 1

Need to list all the sites under a google domain using Google Apps Script

I need to list all the sites under a google domain using Google Apps Script. I have written a small script, but it only returns the sites I have created:

function list_sites() {
  var doc = DocumentApp.create('SiteList1');
  var domain = 'test.com';
  var sites = SitesApp.getSites(domain);

  doc.appendParagraph("Top");
  doc.appendParagraph(sites);
  doc.appendParagraph("Bottom");

  doc.saveAndClose(); // Save and close the document
}

How do I get all the sites?

Upvotes: 0

Views: 1383

Answers (3)

askvictor
askvictor

Reputation: 3819

https://gist.github.com/eduvik/46bb72f3ffea57402b3f

Needs to be run as an admin user

function listSites() {
  var domain = UserManager.getDomain();
  var PAGE_LENGTH=200;
  sheet = SpreadsheetApp.getActiveSheet();
  var sites = SitesApp.getAllSites(domain,0,PAGE_LENGTH);
  for(var i=0; sites.length != 0; i+=PAGE_LENGTH){
    for (var j=0; j<sites.length; j++) {
      sheet.appendRow([sites[j].getUrl()]);
    }
    sites = SitesApp.getAllSites(domain, i, PAGE_LENGTH);
  }
};

Upvotes: 0

Taifun
Taifun

Reputation: 6293

The example Peter mentioned, did not work for me, see also this question: Why does SitesApp.getSites(myDomain) not work? But I found a solution which also might help you:

function list_sites() {
  var doc = DocumentApp.create('SiteList1');
  var domain = 'example.com';
  var site = SitesApp.getSite(domain);

  doc.appendParagraph("Top");
  var pages = site.getAllDescendants();
  for (i in pages) {
    doc.appendParagraph(pages[i].getTitle());
  }  

  doc.appendParagraph("Bottom");
  doc.saveAndClose(); // Save and close the document  
}

Upvotes: 0

Peter
Peter

Reputation: 5601

You can use the getAllSites method.

SitesApp.getAllSites(domain, start, max)

I'm not sure why it's seemingly undocumented and not in the API doc but you can find references to it and I have production scripts that use it daily.

Upvotes: 2

Related Questions