Get list of Team Drives

41 views
Skip to first unread message

Andrea

unread,
May 31, 2019, 4:30:52 AM5/31/19
to Google Apps Script Community
Dear,
I would like to have the list (name and ID) of all the team drives for which I am responsible. 

I've tried it in many ways but I'm not able to

Can you help me please ?
Thank you very much

Andrea

Andrea

unread,
Jun 3, 2019, 4:57:57 AM6/3/19
to Google Apps Script Community
Hi everyone. Excuse me if I insist ... no one can help me please? 

I would like to get, through App Script code, the same result as the GAM command:
gam user and...@mydomain.com print teamdrives


Thank you and good day.
Andrea

Stéphane Giron

unread,
Jun 3, 2019, 6:06:59 AM6/3/19
to Google Apps Script Community
Hi

You have to enable a service account on the domain with wide domain delegation to query the Drive API and list all team as the user.

There is no admin method to get the user team drive list.

Stéphane

Andrea

unread,
Jun 3, 2019, 6:14:40 AM6/3/19
to Google Apps Script Community
Dear Stéphane, thanks for your reply.

Sorry ... I misunderstood.

I don't need to have a list of other users' TeamDrives

I need to have a list (name and ID) of my TeamDrives.

Thanks for your help
Andrea

Stéphane Giron

unread,
Jun 3, 2019, 6:27:13 AM6/3/19
to Google Apps Script Community
Ok in this case something like that must work :

Script must be launched from a script file attached to a google sheets.

function listAllTeamDrive() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
  var teamDrives = [];
  var pageToken,pages;
  do {
    pages = Drive.Teamdrives.list({
      maxResults: 100,
      pageToken: pageToken,
    });
    if (pages.items && pages.items.length > 0) {
      for (var i = 0; i < pages.items.length; i++) {
        var team = pages.items[i];
         Logger.log(team);
        teamDrives.push(teamDriveJsonToArray(team))
      }
    } else {
      Logger.log('No folders found.');
    }
    Logger.log(team);
    pageToken = pages.nextPageToken;
  } while (pageToken);
  
  sheet.getRange(sheet.getLastRow()+1,1,teamDrives.length,teamDrives[0].length).setValues(teamDrives)
}

function teamDriveJsonToArray(team){
  var tab = [team.name];
  
  if(team.capabilities){
    var obj = [team.createdDate,team.capabilities.canEdit,team.capabilities.canCopy,team.capabilities.canComment,team.capabilities.canDownload,team.capabilities.canShare];
  }else{
    var obj = new Array(6)
  }
  tab.concat(obj)
  return tab;
}

Andrea

unread,
Jun 3, 2019, 7:01:04 AM6/3/19
to Google Apps Script Community
WOW !!!
Work perfectly .

Many Many Thanks for your help!

Just a small thing (but really.... without any commitment!)....

Your valuable code produces the list with only the name of the TeamDrive
This the line--> sheet.getRange(sheet.getLastRow()+1,1,teamDrives.length,teamDrives[0].length).setValues(teamDrives)

Is it also possible to add the ID?

I did it this way but it's quite slow

for (var i = 0; i < pages.items.length; i++) {
        var team = pages.items[i];
        sheet.appendRow([team.name, team.id]);
        teamDrives.push(teamDriveJsonToArray(team))
      }


Really, if it's not too much trouble for you. You have already been very kind.

Best regards
Andrea

Stéphane Giron

unread,
Jun 3, 2019, 7:26:09 AM6/3/19
to Google Apps Script Community
Replace the last function by this ne n this case :
function teamDriveJsonToArray(team){
  var tab = [team.name,team.id];
  
  return tab;
}

Andrea

unread,
Jun 3, 2019, 8:17:02 AM6/3/19
to Google Apps Script Community
Sthephane you have been extremely kind.
I wouldn't know how to thank you.
You helped me a lot!

I can only offer you a virtual coffee :)

Have a nice day!!!

Ciao e grazie !
Andrea

Stéphane Giron

unread,
Jun 3, 2019, 8:39:46 AM6/3/19
to Google Apps Script Community
Pleasure

Good afternoon.

Stéphane
Reply all
Reply to author
Forward
0 new messages