Tuesday 21 April 2020

Drive API - share a file without email notification

The following Google Apps Script makes use of the Drive API to allow an item to be shared (eg edit access) without the recipient receiving the automated email typically generated when using the DriveApp.

Enable Drive API
In Script Editor go to Resources > Advanced Google services ... > scroll down and turn on Drive API > click OK.

The Code
First we create an array of objects that defines 3 items: value, type and role. These details will form the 'permissions resource':
var resource = {
    // enter email address here
    value: 'user@example.com',

   
    // choose from "user" or "group" if using a Google Group
    type: 'user', 
              
   
    // choose from: "owner", "writer" or "reader"
    role: 'writer'              
};
Next we create our 'optional arguments' where we can disable the sending of the email:
var optionalArgs = {
    sendNotificationEmails: false
};
Finally we can put this all together with the addition of our File ID to run the command:
Drive.Permissions.insert(resource, 'FILE ID HERE', optionalArgs);
Drive API - share a file without email notification

No comments:

Post a Comment