How to Share Files in Google Drive with Multiple Users in easy way

Hey, friends today I will teach you How to Share Files in Google Drive with Multiple Users in easy way. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Share Files in Google Drive with Multiple Users in easy way.

How to Share Files in Google Drive with Multiple Users in easy way

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

For example, here’s some code that will allow you to share the file with another Google Account user and grant them edit access to the file. To give them read-only access, change their role from writer to reader.

You might also like our trending code snippets

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.

How to Share Files with Multiple Users

The Drive API has a limitation in that you can only share files with one user at a time. Because Google Apps Script does not support the async/await pattern of JavaScript Promises, you 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);
};

Instead of using App Script’s DriveApp service, we directly invoke the Google Drive API (v3) in the snippet above. The fetchAll method performs 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",
    ],
   ...
  }

If this help you please also share it with your friend who need help because your one share can motivate us a lot thankyou.

Leave a Comment