Search code examples
google-apps-scriptgoogle-drive-api

DO NOT send notifications when share a file


I made a code that copy and paste files for a folder to other, and that files are shared with other people, people that can edit o view the files.

I need when the copy and the permissions are copied to the new file, this DON'T sent the email notificactions to editors and viewers.

The first code that U used was this:

  var PeopleToEdit = file.getEditors();
  var PeopleToView = file.getViewers();
      for (var i=0 ; i < PeopleToEdit.length; i++){
      newFile.addEditor(PeopleToEdit[i]);}
      for (var i=0 ; i < PeopleToView.length; i++){
      newFile.addViewer(PeopleToView[i]);}

But then, for the email thing I was trying with this:

function addEditors(file1, file2){
var file = DriveApp.getFileById(file1);
var PeopleToEdit = file.getEditors();
for (var i=0 ; i < PeopleToEdit.length; i++){
       
 Drive.Permissions.insert(
  {
   'role': 'writer',
   'type': 'user',
   'value': PeopleToEdit[i].getEmail()
  },
  file2,
  {
   'sendNotificationEmails': 'true'
  });

In my local drive works, but when I used it in the shared drive, show this error, I don't have any idea what can be.

GoogleJsonResponseException: API call to drive.permissions.insert failed with error: File not found: 1-qsWWwWZI2f0phJW4AO_LLP5fLn2Ica_MhkUClM-s-E


Solution

  • Two things

    1. When you create a Drive permission for a file on a shared drive, you need to set the parameter supportsAllDrives to true

    2. If you do NOT want to send notifications, you need to set sendNotificationEmails to false

    Sample request:

    Drive.Permissions.insert(
      {
       'role': 'writer',
       'type': 'user',
       'value': PeopleToEdit[i].getEmail()
      },
      file2,
      {
       'supportsAllDrives': true, 
       'sendNotificationEmails': false
      });