google-apps-scriptgoogle-sitesgoogle-sites-2016

Get app_id from google sites


I want to grant access for my sites by the script, for google docs I can get the id from the url: https://docs.google.com/document/d/<doc_id>/edit
and do this: DriveApp.getFileById('<doc_id>').addViewer(emailid);

but for google sites I get this format instead:
https://sites.google.com/d/<id_1>/p/<id_2>/edit
I tried use id_1 and id_2 but both does not work.

Can anyone help me?

Thank you


Solution

  • The correct File Id to be used for Google Sites is the <id_1>. The reason why you cannot add viewers to the site is because unlike Google Docs, Google Sites doesn't have a viewer sharing option. Instead, it uses a published viewer option.

    Sample: 3 Users with different access rights (I'm using new Google Sites in this example)

    enter image description here

    Sample Code:

    function myFunction() {
      Logger.log(DriveApp.getFileById('id_1').getViewers());
      Logger.log(DriveApp.getFileById('id_1').getEditors());
    }
    

    Logs:

    [21-01-07 00:52:50:404 HKT] []
    [21-01-07 00:52:50:719 HKT] [DriveUser]
    

    If you want to set viewers for your Google Site, you can use Sites Service in Apps Script. However, you can only use Site.addViewer(emailAddress) for classic sites.

    Note: A rebuilt version of Sites was launched on November 22, 2016. Apps Script cannot currently access or modify Sites made with this version, but script can still access classic Sites.

    (Update)

    Currently, there is no other option to update Google Sites via Apps Script or API.

    Based on this reference: When will available the API Google Site?

    The new version of Google Sites does not offer an API at the moment but Google has announced that API capabilities including Google Apps Script integration will be available.

    For more detailed information see Deprecation Timeline - classic Google Sites

    Google doesn't offer dates for when features will be available so you won't find one.