There are times when you might just want to share a user on a published Google Site as a part of a Google Apps Script Automation. Say you have an internal Google Site for a project and you want to share it with a member as a part of the onboarding process. Alternatively, you might want to share your site as soon as you receive a Stripe payment webhook ping.
To share a user in Google Sites you need to select the share icon from the top menu of the site editor. Then in the dialogue box add the user’s email and choose Published Viewer.
While there is no specific Google Apps Script API to manage Google Sites, we can share permissions and accessibility with Google Drive.
Table of Contents
The Example Google Site
All Google Sites are stored within your Google Drive or Shared Drive. In the example below we have an example Google Site called, Test Site, in the Share a user’s on a published Google Site.

Our aim here is to share the site with a user using their email address.
Keep a note of the folder ID. This will come in handy in the following script.
Extract the Google Site ID from the Edit URL
In my research for this tutorial, I discovered that the Google Drive ID for the site is the first unique ID string in the Google Site Edit URL for example:
https://sites.google.com/d/133BMwNpoKRAjB3KOSLnqDFpEAMELyZrRA/p/1UknqDnw4JqbnZu-63pJvcvFgrOVnkrsm/edit
Get the Google Drive Site ID with Apps Script
I discovered this by using the Google Apps Script DriveApp class to log all site files in my selected folder. Check out the script below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/** * Gets the Site ID from the selected folder. * @see {@link https://yagisanatode.com/how-to-share-a-user-on-a-published-google-site-with-google-apps-script/} for further information. * @author Yagisanatode.com <admin@yagisanatode.com> */ function getSiteIdFromFolder() { const folderID = "2cMPXUgHB7RImY_JRLyKWI6sFTWijSNkh"; const folder = DriveApp.getFolderById(folderID); const files = folder.getFiles(); while(files.hasNext()){ const file = files.next() if(file.getMimeType() === MimeType.GOOGLE_SITES){ console.log(file.getName(), file.getId()); } }; }; |
This returned:
Test Site 133BMwNpoKRAjB3KOSLnqDFpEAMELyZrRA
A match for our first string of characters in the Site Editor URL.
I left this as part of the tutorial because it may be useful if you are iterating folders for selected sites and sharing users.
Code Walkthrough
Line 7 – We set our folder id constant variable to the ID of the folder we need to search.
Line 9 – Here we use the getFolderById() method of the DriveApp class. This method takes the folder ID as an argument. This will return a folder constructor.
Line 10 – Next, we get a list of all files with the getFiles() method. The method returns a file iterator or a list of files to which we can apply file methods.
An alternate approach here would be to use the searchFiles() method to only look for Google Site files.
Line 12 – Now we can use a while loop on each file iteration. We can use the hasNext() method to check if there is another item in the list of files. If there is, then we will select the next file.
Line 13 – The next file is selected with the next() method. For convenience, we set this to the variable file
. This will allow us to access the methods for this file.
Lines 15-16 – Finally, we check to see if the file has a mime type (File Type) of Google Site. If it does, then we log the file name and file id.
Share a user on a Published Google Site
If we have a Google Edit Site URL or ID and an email to share, we can share a user on a Published Google Site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * Gives a user view permissions to the published site. */ function addPublishedViewerToSite(){ const email = "mrsyagisanatode@gmail.com"; const site = "https://sites.google.com/d/133BMwNpoKRAjB3KOSLnqDFpEAMELyZrRA/p/1UknqDnw4JqbnZu-63pJvcvFgrOLnkrsm/edit"; const siteID = site.match("d\/(.*?)\/")[1]; DriveApp .getFileById(siteID) .addViewer(email) }; |
Here, replace the values in the email
and site
variables with your own.
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
Code Walkthrough
Lines 6-7 – Add our email and Google Site Editor URL to these variables.
Line 10 – Here we use the JavaScript Match method and a regular expression to extract the Google Drive Site ID from the URL.
d\/
– You can see the d/ pattern in the URL. We need to escape the forward slash (/) with a backslash (\) so that we don’t have any regular expression errors.(.*?)
– Curly braces indicate the group we want to capture..
– The period matches any character except for a new line.*?
– The star and question mark match the previous value, in our case the period indicating all characters, best zero and an unlimited number of occurrences.
\/
– This indicates that we want to find the next forward slash value.
Match returns an array. The element in position 1 of the array will display all values within the capture group only. This will be our file ID.
Lines 12-14 – We can then use the DriveApp Class to get the Google Site file. From here we can use the addViewer() method to add the viewer’s email as a parameter of the method.
This will result in a user being added as a ‘viewer’ to the Google Site rather than a ‘Published Viewer’. After testing, I can confirm that the ‘Viewer’ permission allows the users to only see the published site and not see the editor site. As such, it is effectively the same as the ‘Published Viewer’ setting.
Incidentally, you can use add multiple users with this approach with the addViewers() method (Note the pluralisation).
You can also share a user as an editor to the Google Site Editor with addEditor().
Using Drive Advanced service to share a viewer
You can also use Google Drive API Advanced Service to add a viewer (Known as ‘Reader’ in Drive API). This approach has the benefit of preventing notifications from being sent to the shared user, should you prefer to send your own email as a part of your automation process.
Note. Google Apps Script users version 2 of the Google Drive API and not the current version 3.
Remove lines 12-14 of the previous code and replace them with the following:
1 2 3 4 5 6 7 8 9 10 11 |
Drive.Permissions.insert( { "value": email, "type": "user", // group, domain, anyone "role": "reader" //editor }, siteID, { sendNotificationEmail: false } ) |
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
Code Breakdown
Line 1 – First, we make a call to the Drive API Permissions resource. And use the insert
method to add our user. In Google Apps Script, theinsert
method takes up to 3 parameters.
Lines 2-6 – The first parameter contains the request body or how you want to set your permissions.
value
: This is the user’s email address.type
: set this touser
if you are sharing a user orgroup
if you are sharing a group email. Alternatively, you can set this todomain
oranyone
if you want to apply this to the entire Google Workspace domain or anyone on the internet respectively.role
: For this tutorial, we want to add users so that they can view the Google Site so we use thereader
role. You can also set the role toeditor
here if you need to add a site editor.
Line 7 – The second parameter is the site id, known as the ‘File ID’.
Lines 8-10 – Finally, we can add a list of optional arguments. In our example, we turned off our email notifications. If you do want to send an email notification you can set this notion to true and then add emailMessage
and add your own customised email message.
Conclusion
That’s it for adding published viewers to a Google Site Using Google Apps Script.
I am always interested in how people use these code snippets in their own projects. I would love to hear your use-case in the comments below. Your ideas really spawn creativity in others.
Need help with Google Workspace development?
Go something to solve bigger than Chat GPT?
I can help you with all of your Google Workspace development needs, from custom app development to integrations and security. I have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.
Schedule a free consultation today to discuss your needs and get started or learn more about our services here.
Happy coding!
~ Yagi