Manage Shared Drives in Google Drive with Google Apps Script

These code samples show how you can use Google Apps Script to manage and search through the content of shared drives in Google Drive using the Drive API.

To get started, click the + icon in the Services section to add the Drive API search to your Google project. Google Apps Script currently supports Drive API v2 though the latest version is v3.

Google Drive API Service

Once the Drive API service is enabled, you can use the Drive API to search through the content of shared drives.

Create a Shared Drive

function createSharedDrive() {
  const driveName = 'Digital Inspiration';
  const sharedDrive = Drive.Drives.insert({ name: driveName }, Utilities.getUuid());
  console.log('Shared Drive created', sharedDrive.id);
}

Share a Shared Drive with a User

function shareSharedDriveWithUser({ driveId, role, email }) {
  // role can be writer, reader, organizaer or commenter
  const response = Drive.Permissions.insert(
    {
      role: role,
      type: 'user',
      value: email,
    },
    driveId,
    {
      supportsAllDrives: true,
      sendNotificationEmails: true,
      fields: 'emailAddress,role',
    }
  );
  console.log('Shared Drive shared with %s', response.emailAddress);
}

Please note that you can only share Shared Drive with Google accounts. The API will not throw an exception if you try share a Shared drive with a non-Google account.

List all Shared Drives

Print a list of all Shared Drives that are accessible to the current user.

function listSharedDrives() {
  let pageToken = null;
  const response = [];

  do {
    const { items = [], nextPageToken = null } = Drive.Drives.list({
      pageToken,
      maxResults: 50,
      orderBy: 'name',
      fields: 'nextPageToken, items(id, name)',
    });
    items.forEach((item) => response.push(item));
    pageToken = nextPageToken;
  } while (pageToken !== null);

  console.log(response);
}

List Files in a Shared Drive

In the next example, we’ll print a list of all files contained in a specific Shared Drive identified by its drive ID that we retrieved in the previous example.

function listFilesInSharedDrive(teamDriveId) {
  let pageToken = null;
  const response = [];

  do {
    const { items = [], nextPageToken = null } = Drive.Files.list({
      pageToken,
      maxResults: 50,
      supportsAllDrives: true,
      includeItemsFromAllDrives: true,
      q: `'${teamDriveId}' in parents and trashed = false and mimeType != 'application/vnd.google-apps.folder'`,
      fields: 'nextPageToken, items(id,title,mimeType)',
    });
    items.forEach((item) => response.push(item));
    pageToken = nextPageToken;
  } while (pageToken !== null);

  console.log(response);
}

Move Files in Shared Drives

Files contained in a specific Shared Drive can be moved to another Shared Drive or to another folder in the same Shared Drive depending on permissions.

function moveFilesBetweenSharedDrives({ parentFolderId, destinationFolderId, fileId }) {
  const data = Drive.Files.update({}, fileId, null, {
    addParents: destinationFolderId,
    removeParents: parentFolderId,
    supportsAllDrives: true,
    fields: 'title,embedLink',
  });
  console.log('File Moved', data.title, data.embedLink);
}

The getCanMoveItemOutOfDrive() method can be used to determine whether the current user can move this item outside of this drive by changing its parent.

Copy Files in Shared Drives

The next snippet illustrates how you can copy files from one Shared Drive to another or between folders of the same Drive. The destinationFolerId is the ID of the folder where the file will be copied to.

function copyFilesInSharedDrives({ title, destinationFolderId, fileId }) {
  const data = Drive.Files.copy({ parents: [{ id: destinationFolderId }], title }, fileId, {
    supportsAllDrives: true,
    fields: 'title,embedLink',
  });
  console.log('File Copied', data.title, data.embedLink);
}
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.