How to Share a User on a Published Google Site with Google Apps Script

Share a user as a viewer on a published google site with apps script

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.

Give Published Viewer access to a Google Site

While there is no specific Google Apps Script API to manage Google Sites, we can share permissions and accessibility with Google Drive.

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.

Google Drive Folder Containing a Google Site File
Click to Expand!

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:

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.

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:

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 to user if you are sharing a user or groupif you are sharing a group email. Alternatively, you can set this to domain or anyoneif 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 the readerrole. You can also set the role to editorhere 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.

Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get you from the basics to a real Google Apps Script pro.

Got a more specific problem you need help with, but don’t have the time to develop the skills? Make an enquiry on my 'Hire me!' page. I occasionally pick up projects. Alternatively, Fiverr’s your best bet to find a skilled Google Apps Script developer to solve your problem quickly and professionally. *

*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.

 

Happy coding!

~ Yagi

Leave a Reply

%d bloggers like this: