If your organisation is using Google Workspace Business Standard, Business Plus, Enterprise, or one of the other supported plans, you are likely taking advantage of the power of Google’s Shared Drives.
If you have decided to create a Google Apps Script project that needs to get a list of your Shared Drive (or a user’s shared drives in the case of a WebApp), then you might be scratching your head right now wondering how to get this list using the built-in DriveApp class.
Whelp, unfortunately, at the time of writing this article the DriveApp class does not have this functionality. However, it is pretty easy to access in a single line of code using an Advance API.
Here’s what you need to do:
As always, read what you need and skip the rest.
Table of Contents
Enable Drive Advanced API
We are going to use the Drive Advance API here.
In your Google Apps Script IDE sidebar (Second sidebar from the left), Select the Services plus button.
A dialogue box will appear with a list of advanced services. Scroll through the list until you find the one marked, Drive API. Then click Add.
To confirm that the API has been loaded, you will see the API displayed now in the sidebar:
The code
The code is an incredibly simple single liner. Check it out:
1 |
const sharedDrive = Drive.Drives.list() |
This will return your list of shared drives as an array of objects like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let sharedDrive = { kind: 'drive#driveList', items: [ { id: '0aIBJOI$oBIOS_OSDIF', kind: 'drive#drive', name: 'Register for Goolge NEXT con OCT 12-14,21' }, { name: 'Goat Appreciation Committee', kind: 'drive#drive', id: 'OA98NSDJK_SKJSsdjFU' }, { id: 'a9od9SO939B8LSDKJ_S', kind: 'drive#drive', name: 'The Goat Float design body' } ] } |
To access Shared drives we call the Drive.Drives method. This method has a list of actions like create, delete, hide, unhide, get and, of course, list.
The list method accepts the query or “q” parameter if you want to refine your search of shared drive. You can get a list of query operators for Shared Drive here.
Note! This approach will only display the first 10 shared drive items. This is generally fine for most cases, but if you just loves your shared drives you will need some more parameters.
Displaying more than 10shared drives
We can access more than 10 shared drive items at a time by using custom parameters for our list call.
Set a larger number
If we know that our users will not have a list of shared drives greater than a set number that is less than 100 shared drives we can add the maxResults
parameter to the list()
method.
1 2 3 4 5 |
const sharedDrives = Drive.Drives.list( { maxResults: 50 } ) |
Iterate over the drive list
Let’s say I only want to see 5 drive items at a time. First, we would call the drives list with a max result of 5. If there are more than 5 items in the list. This sharedDrive
list will contain a nextPageToken
property.
We can then use a while loop, checking if there is a page token. If there is we want to call the drives list again this time with the parameter pageToken with our sharedDrives.nextPageToken
to get the next set of items in the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let sharedDrives = Drive.Drives.list({maxResults: 5}) let sharedDrivesItems = sharedDrives.items; // If a next page token exists then iterate through again. while(sharedDrives.nextPageToken){ sharedDrives = Drive.Drives.list( { pageToken:sharedDrives.nextPageToken } ) sharedDrivesItems = sharedDrivesItems.concat(sharedDrives.items) } |
Just the shared drive info, please
If you look at the returned results above, you will notice that it contains two properties, kind and items. There really isn’t anything important in the kind property to you may as well reference straight to the items property.
1 |
const sharedDrive = Drive.Drives.list().items |
And will store this:
1 2 3 4 5 6 7 8 9 10 11 |
const sharedDrive = [ { id: '0aIBJOI$oBIOS_OSDIF', kind: 'drive#drive', name: 'Register for Goolge NEXT con OCT 12-14,21' }, { name: 'Goat Appreciation Committee', kind: 'drive#drive', id: 'OA98NSDJK_SKJSsdjFU' }, { id: 'a9od9SO939B8LSDKJ_S', kind: 'drive#drive', name: 'The Goat Float design body' } ] |
This gives you an array of objects, with shared drive data in each array item.
Time to be a little more unkind
Again the kind data for each of your shared drive items are pretty useless. So you might want to map the array to remove it.
1 2 |
const sharedDrive = Drive.Drives.list().items .map(drive => ({id:drive.id,name:drive.name})); |
Which will give you:
1 2 3 4 5 6 7 8 |
const sharedDrives = [ { id: '0aIBJOI$oBIOS_OSDIF', name: 'Register for Goolge NEXT con OCT 12-14,21' }, { name: 'Goat Appreciation Committee', id: 'OA98NSDJK_SKJSsdjFU' }, { id: 'a9od9SO939B8LSDKJ_S', name: 'The Goat Float design body' } ] |
Watch the video
Conclusion
That’s really all there is to it.
Once you have the shared Drive ID you can head back to the safety of DriveApp and use the id to retrieve, save and delete files in these shared drives.
I use this code snippet when creating web apps that are accessed by users with a Google account so that their data can be saved in these locations. I’ve also used this as a location reference using the Google Picker tool.
Finding out who the creator of the Shared Drive can be a little tricky check out the post below for a tutorial:
Get the Creator’s Email of a Shared Drive with Google Apps Script
Need help with Google Workspace development?
Go something to solve bigger than Chat GPT?
I can help you with all of your Google Workspace development needs, from custom app development to integrations and security. I have a proven track record of success in helping businesses of all sizes get the most out of Google Workspace.
Schedule a free consultation today to discuss your needs and get started or learn more about our services here.
~ Yagi
Changelog
- 16 Dec 2022 – Added a link to accessing the email of the creator of the Shared Drive.
- 18 Feb 2022 – Updated ‘The Code’ chapter to acknowledge the default list value of drives displayed is 10. Added sub-chapters on increasing the list returned list item numbers with maxResults and page tokens.
How can you get a list and/or do a google search of more than 100 items to find all google sheets and/or Google stock sheets that are shared, and can be edited.
Hi Ron,
The DriveApp searchFiles() method is probably what you are looking for. You can find a list of search options here.
~Yagi
Hi Yagi
Thanks for the great content you have here.
Is there any way to obtain a list of ID and Name of all files/folders in a Google Shared Drive, using the ID of the Shared Drive ?
Thanks !
Hi Yagi
Thanks for the great content you have here.
Is there any way to obtain a list of ID and Name of files/folders in a Google Shared Drive, using the id of the Shared Drive ?
Thanks
Hi Yagi, thanks for this, I successfully followed your instructions and got the id of my shared drive.
The part I am confused about is this:
“Once you have the shared Drive ID you can head back to the safety of DriveApp and use the id to retrieve, save and delete files in these shared drives.”
I don’t see a way to do that in the DriveApp documentation. Can you provide a quick explanation? Thanks!
Hi Gaby,
Let’s say we used this approach:
We could get the folder name of the first item in sharedDrive using:
const folder = DriveApp.getFolderById(sharedDrive[1].id)
You could then use this like a normal folder instance.
Does this make sense? Should I add this example to the tutorial?
~Yagi