Tuesday 1 June 2021

Set permissions on a Shared drive

The following Google Apps Script is designed to bulk set permissions on a Shared drive. It has been created as a standalone Function that requires the ID of the Shared drive and manually entered email addresses for the relevant permsisions.

This is how I first worked with it to learn what information was required and how to structure the content, before combining it all into a Google Sheet tool which will be blogged about in the near future.


Enable Drive API Service

Make sure you have followed the instructions here to enable the Drive API Service.

 

The Code

Below are some comments about snippets of the code to better understand what is happening.

We need to create a JavaScript Object that contains the relevant Shared drive permissions and email addresses as arrays. There are some blank values in this example to show that not every type of role needs something in it:

var roles = {
    organizer: ["
example1@example.com"], // Manager
    fileOrganizer: ["example2@example.com"], // Content Manager
    writer: ["example3@example.com", "example4@example.com"], // Contributor
    reader: [ ] // Viewer
};

Next we are going to loop through each of the above roles, assign a key for each (eg 'organizer', 'fileOrganizer') and then go through each role and create a resource for adding permissions:

// go through the above roles
for (var key in roles) {


    // assign a key for 'organizer, fileOrganizer, ...
    var role = roles[key];


    // go through each role and create a resource for adding permissions
    role.forEach(function (email) {
      var resource = {
        role: key,
        type: "user",
        value: email
      }

Now we create an optional argument to not send notification emails and to support all types of Google Drives:

var optionalArgs = {
        sendNotificationEmails: false,
        supportsAllDrives: true
}

Finally we make the call to the Drive API to insert all of our permissions:

Drive.Permissions.insert(resource, sharedDriveID, optionalArgs);

Unfortunately in version 2 of the Drive API there is a slightly more cumbersome way of adding a 'Commenter' which involves an additionalRoles parameter being added to the resource. This method is included in the below GitHub code however and in the downloadable file.


Download

Set permissions on a Shared drive download here (please use 'File' > 'Make a copy' for your own version).

No comments:

Post a Comment