Get a list of Google Shared Drives by ID and Name in Google Apps Script [updated 10 Feb 2022]

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.

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.

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.

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

Changelog

  • 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. 

6 thoughts on “Get a list of Google Shared Drives by ID and Name in Google Apps Script [updated 10 Feb 2022]”

  1. 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.

    1. Hi Ron,

      The DriveApp searchFiles() method is probably what you are looking for. You can find a list of search options here.

      ~Yagi

  2. 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 !

  3. 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

  4. 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!

    1. 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

Leave a Reply

%d bloggers like this: