2

I have some Google Apps script code that searchs for files and folders on TeamDrive.

The issue I am having is that if a file or folder is created by my colleague, when I run my script it can't find the file. If I create a file, and my colleague runs the script, the script can't find the file even though we both have access to view, edit and can see the files and folders in Drive. If one of us edits the file made by the other person, then it becomes visible from the search.

I ran into a similar problem with the Drive REST api when doing some android development. In Android when calling files().list(), It took my a while to find out that I had to set the following in order for my search to be successfull every single time.

  1. .setSupportsTeamDrives(true)
  2. .setIncludeTeamDriveItems(true)
  3. .setCorpora("teamDrive")
  4. .setTeamDriveId(myFolder.getTeamDriveId())

I assume I am running into the same issue with my apps script code.

//Create the N Google docs files
function CreateNFiles(){
  var spreadsheet = SpreadsheetApp.getActive();
  var Nmain = spreadsheet.getSheetByName("Nmain")
  var spreadsheetId = spreadsheet.getId();
  var pdfDir = "Form Data";
  var TemplatesFolder = null;

  //Check and see if there is a 'Form Data' folder
  var NFolderId = null;
  var RFolderId = DriveApp.getFileById(spreadsheetId).getParents().next().getId();
  var files = DriveApp.searchFolders('parents="'+RFolderId+'" and trashed=false');
  while (files.hasNext()) {
    var myfile = files.next();
    if(myfile.getName() == pdfDir){
      NOFolderId = myfile.getId();
    }
  }

https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)

this says to refer to

https://developers.google.com/drive/api/v3/search-parameters#examples_for_teamdriveslist

so I could potentially use

corpora="teamDrive"

is there a way to setSupportsTeamDrives? and setIncludeTeamDriveItems? and setTeamDriveId? in google apps scripts

Trevor
  • 117
  • 1
  • 12

3 Answers3

4

Finding Files and Folders in a Team Drive

Here's a couple of functions I've been working on for my own needs. They're still a work in progress but one can file folders within a team drive folder and another can find items within a team drive folder. The Logger.log is setup to display item number, title, id, and mimeType.

This one finds Items (either files or folders). You can tell them apart by their types.

function findItemsInTeamDriveFolder(teamDriveId,folderId){
  var teamDriveId=teamDriveId || '0AFN5OZjg48ZvUk9PVA';
  var folderId=folderId || '1LK76CVE71fLputdFAN-zuL-HdRFDWBGv';
  var options={
    "corpora":"teamDrive",
    "includeTeamDriveItems":true,
    "orderBy":"folder",
    "q":Utilities.formatString('\'%s\' in parents',folderId),
    "supportsTeamDrives":true,
    "teamDriveId":teamDriveId
    };
  var files=Drive.Files.list(options);
  var data=JSON.parse(files);
  for(var i=0;i<data.items.length;i++){
    Logger.log('\nItem: %s - Title: %s - Id: %s - Type:%s - Trashed: %s\n',i+1,data.items[i].title,data.items[i].id,data.items[i].mimeType,data.items[i].explicitlyTrashed?'true':'false'); 
  }
}

This one just finds folders in a folder. It's not reentrant it's a one level deal but currently that's all I need.

function findFoldersInATeamDriveFolder(teamDriveId,folderId){
  var teamDriveId=teamDriveId || '0AAc6_2qyI7C0Uk9PVA';
  var folderId=folderId || '1HenWOXTSCg96iAvA0ZkgEA9EGKlch4fz';
  var optionalArgs={
    "corpora":"teamDrive",
    "includeTeamDriveItems":true,
    "orderBy":"folder",
    "q":Utilities.formatString('\'%s\' in parents and mimeType = \'application/vnd.google-apps.folder\'',folderId),
    "supportsTeamDrives":true,
    "teamDriveId":teamDriveId
  }
  var list=Drive.Files.list(optionalArgs)
  var data=JSON.parse(list);
  for(var i=0;i<data.items.length;i++){
    Logger.log('\nItem: %s - Title: %s - Id: %s - Type: %s - Trashed;%s\n',i+1,data.items[i].title,data.items[i].id,data.items[i].mimeType,data.items[i].explicitlyTrashed?'true':'false'); 
    findItemsInTeamDriveFolder(teamDriveId,data.items[i].id)
  }
}

I thought that they might be helpful.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks, I was hoping to avoid using REST api.......I would like to see if there is a DriveApp solution. But if there isn't thanks! This will help me get going using a REST solution pretty quickly. – Trevor Dec 13 '18 at 19:14
  • I find that learning how to use the RESTful APIs is greatly enhanced by looking at what's available to Apps Script in the content completion. Of course formatting the optionalArg object is still tough to figure out but then you can look at the RESTful documentation for optional parameters. If you run into problems try to make a really concise script to ask your question and hope that Tanaike or tehhowch is around to help you. Tanaike and tehhowch have blessed me with some great answers and examples. – Cooper Dec 13 '18 at 19:49
  • Thanks Cooper! This is essentially what I have done in Java/Android and has worked well for getting a full list of files and folders as required. Its bizarre that they would have DriveApp and it kind of works for TeamDrive......but not fully. They should really be documenting it better indicating that its really only meant for personal drive accounts and not TeamDrive..... – Trevor Dec 13 '18 at 20:22
  • looks like this need an update to deprecated `includeTeamDriveItems`, see https://developers.google.com/drive/api/v2/reference/files/list#includeTeamDriveItems – Kos Sep 08 '21 at 15:29
1

I just used Coopers code to list files that were in a shared drive. I added the code to find the teamdriveID. Two things that cost me some time and might be helpful for others: the number of files is restricted to 100 per default. So I changed it to 200 here. Also, the options file includes trashed files (very confusing) so I filtered them out with an if statement - I am sure this can be done more elegantly but this worked :)

var resource = {
  'value': 'emailstring',
  'type': 'user',
  'role': 'writer'
}

var teamDriveId
// If you have several Team Drives, loop through and give access
  var TeamDrive = Drive.Teamdrives.list();
  for(var i = 0; i < TeamDrive.items.length; i++) {
    if(TeamDrive.items[i].name == "foldernamestring") {
      // This ID may also be a single file inside a Team Drive
      teamDriveId = TeamDrive.items[i].id;
      Logger.log("found "+TeamDrive.items[i].name);          
    }
  }

var options={
        "corpora":"teamDrive",
        "maxResults":200,
        "includeTeamDriveItems":true,
        "supportsTeamDrives":true,
        "teamDriveId":teamDriveId
        };
var files=Drive.Files.list(options);
var data=JSON.parse(files);

var nritems= data.items.length
Logger.log("nritems "+nritems);          
for(var i=0;i<nritems;i++){
        if (data.items[i].explicitlyTrashed == false){
          Logger.log('\nItem: %s - Title: %s - Id: %s - Type:%s - Trashed: %s\n',i+1,data.items[i].title,data.items[i].id,data.items[i].mimeType,data.items[i].explicitlyTrashed?'true':'false'); 
        }
      }
neerda
  • 33
  • 5
0

I use simple Apps Script code to search folders in a Shared Drive:

function searchFolderByTitle(title,ShDrId){
  var folders = DriveApp.searchFolders("title contains '"+title+"' and '"+ShDrId+"' in parents");
  while (folders.hasNext()) {
    var folder = folders.next();
    Logger.log(folder.getName());
  }
}

You should use the operator "in" not "=" with "parents" parameter.

Bav4ik
  • 1