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:
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);
}