Get a list of Google Shared Drives by ID and Name in Google Apps Script

Get a list of Google Shared Drives by ID and Name in Google Apps Script

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. 

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.

Google Apps Script get Drive Advanced API

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.

Google Apps Script get Drive Advanced API selection
Click to Expand!

To confirm that the API has been loaded, you will see the API displayed now in the sidebar:

Google Apps Script get Drive Advanced API

The code

The code is an incredibly simple single liner. Check it out:

This will return your list of shared drives as an array of objects like this:

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.

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.

And will store this:

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.

Which will give you:

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.

Looking to learn more about Google Apps Scripts in a more structured format? Udemy has some great courses that can get you from the basics to a real Google Apps Script pro!

Got a more specific problem you need help with, but don’t have the time to develop the skills? Make an enquiry on my ‘Hire me!’ page. I occasionally pick up projects. Alternatively, Fiverr’s your best bet to find a skilled Google Apps Script developer to solve your problem quickly and professionally. *

*The above affiliate links have been carefully researched to get you to what you specifically need. If you decide to click on one of these links it will cost you just the same as going to the site. If you decide to sign up, I just get a little pocket money to help pay for the costs of running this website.

~ Yagi

Leave a Reply