Find Files in a Shared Drive with Apps Script

Viewed 86

This code search for some files inside a folder. It work good on my drive, but I need it for a shared folder in my company and it does not get file names and ids. I've read about {supportsAllDrives: true} but I don't know how to implement it in the code (I don't even know if its the solution). Drive API and DriveActivity API are enabled in my project.

var expFolder = carpetabasecs.searchFolders(searchFor);
  while (expFolder.hasNext()) {
var expFolderDef = expFolder.next();
var expFolderId = expFolderDef.getId();

var mask = 'TXT';
var query = `title contains "${mask}" and trashed = false and "${expFolderId}" in parents`;
var findings = Drive.Files.list({ q: query }) || [];
var table = [['Archivos exportados', 'IDs'], ...findings.items.map(f => [f.title, f.id]).sort()];

sh.getRange(2,1,table.length,2).setValues(table);

Thanks

3 Answers

Finally I'm using this method to filter the files. It's working good on shared drives.

  var suffix = 'TXT' // adapt as necessary
  var list = [];
  var files = expFolderDef.getFiles();
  while (files.hasNext()) {
    file = files.next();
    list.push([file.getName(),file.getId()])
  }
  var result = [['Archivos exportados','IDs'], ...list.filter(r => r[0].includes(suffix)).sort()]
  sh.getRange(2,1, result.length, result[0].length).setValues(result);

Anyway I'm still interested in understand how to use the Drive.Files.list command, useDomaininAdminAccess and supportAllDrives don't make the work. I've read that the command is in Advanced Drive Service, that API is the same as Drive API? Because I can't find Advanced Drive Service in Cloud Services.

Did you try like this?

var findings = Drive.Files.list( { 
    q: query,
    useDomainAdminAccess: true
  }) || [];

Change this:

var findings = Drive.Files.list({ q: query }) || [];

To this:

var findings = Drive.Files.list({ q: query, supportsAllDrives: true }) || [];

Remember that you are using the Drive v2 (not v3)

Documentation
Related