4

I am writing a JavaScript tool in Google Apps Script to check some properties of documents, like "are all links valid", "are permissions set correctly", and so on. I am using the API documented in https://developers.google.com/apps-script/reference/drive/drive-app to look up files by ID, check their permissions, locate them in Google Drive etc., but I found that "Shared Drives" don't work very nicely with that API.

For example,

  • for the root folder of a Shared Drive, Folder.getName() only returns "Drive" rather than the Drive's name,
  • even though mygroup@domain.com is a "Manager" of the Shared Drive, folder.getAccess('mygroup@domain.com') is NONE and folder.getViewers() is empty,
  • some folders in Shared Drives are not (always) included in the DriveApp.getFolders() iterator.

In particular the second point is a blocker for me now, but what am I missing here? Is there some other API I should be using, or is it simply a bug that I should report? Is there some documentation of what functionality of the Drive API I can and cannot use with Shared Drives?

Rubén
  • 34,714
  • 9
  • 70
  • 166
tgpfeiffer
  • 1,698
  • 2
  • 18
  • 22
  • 1
    Does this answer your question? [Accessing Files and Folders in Team Drive](https://stackoverflow.com/questions/48213373/accessing-files-and-folders-in-team-drive) – Rubén Jun 16 '20 at 14:48
  • 2
    It is related, but I think the main point I was missing is that (1) yes, I *cannot* work with Team Drives using the DriveApp API, and (2) I *can* use the Drive API v2 from within Google Apps Script scripts, which both seem to be already known to the user asking the question in your link. So I think in that regard the question is not exactly a duplicate, and the answer provided by ziganotschka below provided important information to me. – tgpfeiffer Jun 17 '20 at 01:13

1 Answers1

5

Use the Advanced Drive Service instead of DriveApp

  • Indeed, shared drives are not supported by DriveApp which has a limited scope
  • But if you enable the Advanced Drive Service, yuo will be able to use in Apps Script all methods of the Drive API v2 which support shared drives

Sample:

function myFunction() {
  var sharedDriveName = Drive.Drives.get("XXXXXXXXXXXXXXXXXXX").name;
  //it is important to specify that the folder is located on a shared drive with {"supportsAllDrives": true}
  var folderOnDriveName = Drive.Files.get("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",{"supportsAllDrives": true}).title;
  var folderPermissions = Drive.Permissions.list("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",{"supportsAllDrives": true});
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • 2
    I see, I had come across `Drive.Drives` before, but I didn't understand that it's also usable from Google Apps Script once the Advanced Drive Service is enabled, I thought it's only a web API. So now given a `GoogleAppsScript.Drive.File` I can check the permissions with something like `Drive.Permissions.list(f.getId(), {supportsAllDrives: true}).find((p) => p.emailAddress == "mygroup@domain.com");`. Thank you! – tgpfeiffer Jun 17 '20 at 01:08
  • 2
    @tgpfeiffer - please, note for future reference that not only Drive, but all advanced services are [wrappers](https://developers.google.com/apps-script/guides/services/advanced#support_for_advanced_services) around corresponding REST APIs, so you can use them interchangeably – Oleg Valter is with Ukraine Jun 17 '20 at 01:15
  • Where exactly is the documentation on this `Drive.Drives` call you are using? When I go to the "Advanced Drive Service" link you provided, I see `Drive` but nothing under it called "Drives". I see "Drive API" but this only has sample code, none of which contains `Drive.Drives`. When I follow the `Drive API v2` link I likewise find something *hinting* about an API, but I don't see a reference page for a "Drives" object. I also searched using Google and found something about a REST API with JSON but nothing about how this translates into Javascript cals. – Michael May 17 '23 at 22:26