How to Share Files in Google Drive with Multiple Users

H
Published in: Google Apps Script - Google Drive

The Google Drive API makes it easy to share files and folders with other users programmatically with the help of Apps Script.

For instance, here’s a snippet of code that will let you share the file with another Google Account user and provide them edit access to the file. Replace the role from writer to reader to give them read-only access.

const shareFilesInGoogleDrive = (fileOrFolderId, emailAddress) => {
  Drive.Permissions.insert(
    {
      role: "writer", // or "reader" or "commenter"
      value: emailAddress,
      type: "user",
    },
    fileOrFolderId,
    {
      supportsAllDrives: true,
      sendNotificationEmails: true,
    }
  );
};

It is recommended that you set the sendNotifications flag to true as it will send an email notification when the file is shared with a user who may not have a Google account.

Share Files with Multiple Users

A limitation of the Drive API is that you can only share files with one user at a time. Google Apps Script is synchronous - it doesn’t support the async/await pattern of JavaScript Promises and you therefore cannot run the code in parallel.

There’s however a simple workaround to help you share a file or folder in Google Drive with multiple users in one go in parallel using the UrlFetchApp service.

const shareGoogleDriveFileWithMultipleUsers = () => {
  const fileId = "<Drive File Id>";
  const editors = ["angus@gmail.com", "kiran@school.edu", "jacob@corp.com"];

  const API = "https://www.googleapis.com/drive/v3/files";
  const queryString = "supportsAllDrives=true&sendNotifications=true";
  const accessToken = ScriptApp.getOAuthToken();

  const requests = editors.map((emailAddress) => ({
    url: `${API}/${fileId}/permissions?${queryString}`,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessToken}`,
    },
    muteHttpExceptions: true,
    payload: JSON.stringify({
      role: "writer",
      type: "user",
      emailAddress: emailAddress,
    }),
  }));

  UrlFetchApp.fetchAll(requests);
};

In the snippet above, we are directly invoking the Google Drive API (v3) instead of the DriveApp service of App Script. The fetchAll allows you make multiple HTTP requests in a single request and returns an array of responses.

Please ensure that the following scopes are added in your appsscript.json file:

  {
    ...
    "oauthScopes": [
      "https://www.googleapis.com/auth/script.external_request",
      "https://www.googleapis.com/auth/drive",
    ],
   ...
  }
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Google Drive

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube