Search code examples
google-apps-scriptgoogle-drive-apigoogle-drive-shared-drive

How to check whether the file belongs to My Drive or Team Drive


Is there any less time consuming way to check if file's Root Directory is "My Drive" or "Team Drive"?

I have tried using file.getParents(); and then iterating through every parent and finally to Root, but this is way too time consuming.


Solution

  • When a file is in Team Drive, the owner is the Team Drive and not any particular user.

    You can perform a check for ownership on the file and if it is set to null, the file is likely inside Team Drive.

    var file = DriveApp.getFileById("fileID");
    if (file.getOwner() === null) {
      // do something here
    }