0

I am trying to write a simple Google Apps Script to list the files in a Google Team Drive and I am not having much success.

Here is the code:

function start()
{
  Logger.log("Starting application...");
  var startingFolders = DriveApp.getFoldersByName("Temp");
  if (startingFolders.hasNext())
  {
    // Assuming only one folder with that name
    accessFiles(startingFolders.next());
  }
  else
    Logger.log("Folder not found");
}

function accessFiles(folder)
{
  Logger.log("Folder: %s", folder.getName());
  // Print some file properties
  var files = folder.getFiles();
  while (files.hasNext())
  {
    var file = files.next();
    Logger.log("Working on file %s. Current access: %s.", file.getName(), file.getSharingAccess());
    // some work here...
  }

  // Explore subfolders
  while (folder.hasNext())
  {
    var subfolder = folder.next();
    accessFiles(subfolder);
  }
}

The problem is that the log always prints the "Folder not found" message. "Temp" is a folder in a Team Drive that I have Full access to. Note that I am trying to use the Google Apps Script and not the REST API.

I am not sure what I am doing wrong, or if Team Drives are not supported yet...

Any help would be appreciated! Thanks.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Raph
  • 3
  • 3
  • I don't recall anyone mentioning that it's supported yet. Another option is that it could be supported, but scripts from a users drive cannot reach a team drive. Is your script located in the same team drive? – Vytautas Jan 17 '18 at 05:51

1 Answers1

1

It's not possible to search file and folders on a Team Drive by using the Google apps Script Service (DriveApp) but we could use the Drive Advanced Service but first we should enabled it first. The instructions are on Enabling Advanced Services.

The following script will list all the files inside a folder named Temp from an specified Team Drive by its id.

function listFiles(){
  var teamDriveId = 'put_here_the_teamdrive_id';
  var pageToken;
  var folders = Drive.Files.list({  
    corpora: 'teamDrive',
    supportsTeamDrives: true,
    teamDriveId: teamDriveId,
    includeTeamDriveItems: true,
    q: 'title = "Temp"'
  });
  if(folders.items.length !== 1) {
    Logger.log('There is a problem.');
    return;
  }
  var query = 'trashed = false and ' + //to exclude trashed files
      'not mimeType = "application/vnd.google-apps.folder"'; // To exclude folders
  var files, pageToken;
  do {
    files = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken,
      // required for team drive queries
      corpora: 'teamDrive',
      supportsTeamDrives: true,
      teamDriveId: teamDriveId,
      includeTeamDriveItems: true
    });
    if (files.items && files.items.length > 0) {
      for (var i = 0; i < files.items.length; i++) {
        var file = files.items[i];
        Logger.log('%s (ID: %s)', file.title, file.id);
      }
    } else {
      Logger.log('No files found.');
    }
    pageToken = files.nextPageToken;
  } while (pageToken);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thank you very much @Rubén, this is exactly what I was trying to do! No need for a recursive search anymore with this type of sql-like query! – Raph Jan 18 '18 at 20:40