I have a Google App Script which finds files in Google Workspace for my organization which match various search parameters. So far so good.
But, the issue that I am running into is that it is finding not only files in Shared Drives but also files which are apparently in individual users' "My Drive" and I cannot seem to tell which it is in the script.
I'm using DriveApp.searchFiles
to get the file iterator for the files found. I'm doing something like this:
var files = DriveApp.searchFiles(searchParams);
while (files.hasNext()) {
var file = files.next();
<do something with file here>
}
One of the things I tried to do with the file was to call file.getParents()
recursively to determine the full path to the file. My hope was that I could look at the top-level folder and see either "My Drive" or the name of the actual Shared Drive. However, it seems like the root folder is always reported as "Drive" regardless of whether the file is actually in somebody's "My Drive" or is in one of the Shared Drives. There is no "My Drive" or Shared Drive folder name anywhere in the path.
I also tried calling file.getOwner()
. For some reason this is returning null
, even when the file is not in a Shared Drive, which contradicts my understanding that it should be the owner whose "My Drive" the file is in.
I also looked for any way to determine which drive the file is in via the API. However, there is nothing regarding the drive itself.
I also tried adding something to my searchParams
string to would limit the search to only files in a Shared Drive. However, the search guide doesn't seem to provide any way to do this. There is a section on "Shared drive-specific query terms" but no indication on how to access this; for instance, the "name" QueryTerm appears to provide a mechanism to search based on the "Name of the shared drive" however there is no indication how one would use this when there is already a "name" QueryTerm in the "File-specific query terms" section which tries to match the file name.
Using this definition of a file, I would like to know any one of these in a Google App Script, in decreasing order of importance:
- How to determine the name of the Shared Drive the file is in (if applicable)
- How to determine if the file is in a Shared Drive or not
- The search string to add to my searchParams to filter out anything not in a Shared Drive.
Also, I saw this question which has an answer suggesting to use the "Advanced Drive Service" but I couldn't find any documentation showing how to actually use it (e.g. a javascript reference for Drive.Drives). The closest I came was this page which does have v2 hidden under there, but it shows JSON representation for the REST API and no indication how that corresponds to what is presumably a Javascript object Drive.Drives nor how to use it.
Since my call to searchFiles is already returning files from Shared Drives I'm also not sure if this answer is still up to date.