So I'm working on something to list all sites within our domain.
I can get this using Drive.File.list, and that's fine. I can query and get the name of the site and the owners email, however, I can't figure a way to get the sites published URL.
This is the core of what I have so far:
function getNewSites() {
var responses = Drive.File.list({
corpus: "DEFAULT",
maxResults: 1,
q: 'mimeType="application/vnd.google-apps.site" and title="Test Site"',
fields: "items/owners/emailAddress,items/title"
});
Logger.log(responses);
}
Which returns:
{
"items": [
{
"title": "Test Site",
"owners": [
{
"emailAddress": "email@address.co.uk"
}
]
}
]
}
You can get edit URLs and embed links etc, but I can't figure out how to get the published URL at all (Is it's not the file name that is used in that URL)
The URL in that example is https://sites.google.com/domain.co.uk/testsaaaaa/home
'testsaaaaa' isn't referenced anywhere that I can see in the payload.
I'm not sure this can be done with the Drive API, since the Drive API is more or less about generic file properties and not document-specific things. I checked both v2 and v3 using the Google APIs Explorer, and was unable to obtain any sort of "published" information.
Drive v2 demo link
Drive v3 demo link
Unfortunately, the Google Sites API is not (yet?) compatible with the "new" Google Sites. With old sites, you could access this published URL with a web address mapping request: https://developers.google.com/sites/docs/1.0/developers_guide_protocol#WebAddressMappings
You can authorize the Google Sites API by editing your Apps Script manifest file to include the scope "https://sites.google.com/feeds"
This is an example script (that again, will only work for "classic" Google Sites:
function ClassicSitesLister() {
// https://developers.google.com/sites/docs/1.0/reference#feed_ListSites
const url = 'https://sites.google.com/feeds/site/site?with-mappings=true'; // replace final 'site' with custom domain
const options = {
Authorization: "Bearer " + ScriptApp.getOAuthToken()
};
const resp = UrlFetchApp.fetch(url, {headers: options});
const content = resp.getContentText(); // gets XML content
console.log({message: "Classic Sites data", xmlContent: content});
}
And the logged data is similar to this:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gs='http://schemas.google.com/spreadsheets/2006'
xmlns:thr='http://purl.org/syndication/thread/1.0' xmlns:sites='http://schemas.google.com/sites/2008'
xmlns:dc='http://purl.org/dc/terms' xmlns:gAcl='http://schemas.google.com/acl/2007'>
<id>https://sites.google.com/feeds/site/site</id>
<updated>2018-10-08T16:09:52.181Z</updated>
<title>Site</title>
<link rel='alternate' type='text/html' href='https://sites.google.com/feeds/site/site'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://sites.google.com/feeds/site/site'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://sites.google.com/feeds/site/site'/>
<link rel='self' type='application/atom+xml' href='https://sites.google.com/feeds/site/site?with-mappings=true'/>
<generator version='1' uri='http://sites.google.com'>Google Sites</generator>
<openSearch:startIndex>1</openSearch:startIndex>
<entry gd:etag='"KXsmYD5aEw.."'>
<id>https://sites.google.com/feeds/site/site/......</id>
<updated>2015-08-25T20:44:44.336Z</updated>
<app:edited xmlns:app='http://www.w3.org/2007/app'>2015-08-25T20:44:44.336Z</app:edited>
<title>......</title>
<summary>......</summary>
<link rel='alternate' type='text/html' href='https://sites.google.com/site/....../'/>
<link rel='http://schemas.google.com/acl/2007#accessControlList' type='application/atom+xml' href='https://sites.google.com/feeds/acl/site/site/.....'/>
<link rel='edit' type='application/atom+xml' href='https://sites.google.com/feeds/site/site/.....?with-mappings=true'/>
<link rel='self' type='application/atom+xml' href='https://sites.google.com/feeds/site/site/......'/>
<sites:siteName>......</sites:siteName>
<sites:theme>......</sites:theme>
</entry>
</feed>
In the above feed, the <link rel='alternate'>
's href
attribute is the published URL for that particular site. Per the linked example, if you alias to a custom domain that should appear as its own entry under <link rel='webAddressMapping' href='....">
, of which there may be multiple.