How to Share Files in Google Drive with Multiple Users

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",
    ],
   ...
  }
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

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.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.