Advanced Drive Labels Service
Stay organized with collections
Save and categorize content based on your preferences.
Create and manage labels for your Drive files and folders with the Google Drive
Labels advanced service. With this advanced service, you can use all the
features of the
Drive Labels API
in Apps Script.
To apply or remove Drive labels, use the
Advanced Drive Service.
Reference
For more information about this service, see the documentation for the
Google Drive Labels API. Like all advanced
services in Apps Script, the Drive Labels API service uses the same objects,
methods, and parameters as the public API.
To report issues and find other support, see the Google Drive Labels API
support guide.
Sample code
The sample code below uses
version 2 of the
API.
List labels
The following code sample shows how to get a list of labels available to the
user making the request.
Get a label
The following code sample shows how to get a single label by its
resource name
(which is the string value of the label). To find the label name, get the list
of labels through the API or use the Drive labels manager. For more information
on the labels manager, go to
Manage Drive labels.
List labels for a Drive item
The following code sample shows how to get a Drive item and list all labels
applied to that item.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eUtilize the Google Drive Labels advanced service in Apps Script to create and manage labels for your Google Drive files and folders, leveraging the Drive Labels API.\u003c/p\u003e\n"],["\u003cp\u003eTo use this advanced service, ensure you enable the Advanced Drive Service in your Apps Script project settings before implementation.\u003c/p\u003e\n"],["\u003cp\u003eAccess comprehensive documentation and support resources for the Google Drive Labels API, which uses the same structure as the public API, in the provided references.\u003c/p\u003e\n"],["\u003cp\u003eExplore the provided sample code snippets to learn how to list available labels, retrieve specific labels by name, and list labels applied to Drive items using Apps Script.\u003c/p\u003e\n"]]],[],null,["# Advanced Drive Labels Service\n\nCreate and manage labels for your Drive files and folders with the Google Drive\nLabels advanced service. With this advanced service, you can use all the\nfeatures of the\n[Drive Labels API](/drive/labels/guides/overview)\nin Apps Script.\n\nTo apply or remove Drive labels, use the\n[Advanced Drive Service](/apps-script/advanced/drive).\n| **Note:** This is an advanced service that you must [turn on before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor more information about this service, see the documentation for the\n[Google Drive Labels API](/drive/labels). Like all advanced\nservices in Apps Script, the Drive Labels API service uses the same objects,\nmethods, and parameters as the public API.\n\nTo report issues and find other support, see the Google Drive Labels API\n[support guide](/drive/labels/support).\n\nSample code\n-----------\n\nThe sample code below uses\n[version 2](/drive/labels/reference/rest) of the\nAPI.\n\n### List labels\n\nThe following code sample shows how to get a list of labels available to the\nuser making the request. \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * List labels available to the user.\n */\nfunction listLabels() {\n let pageToken = null;\n let labels = [];\n do {\n try {\n const response = DriveLabels.Labels.list({\n publishedOnly: true,\n pageToken: pageToken\n });\n pageToken = response.nextPageToken;\n labels = labels.concat(response.labels);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to list labels with error %s', err.message);\n }\n } while (pageToken != null);\n\n console.log('Found %d labels', labels.length);\n}\n```\n\n### Get a label\n\nThe following code sample shows how to get a single label by its\n[resource name](/drive/labels/reference/rest/v2/labels/get)\n(which is the string value of the label). To find the label name, get the list\nof labels through the API or use the Drive labels manager. For more information\non the labels manager, go to\n[Manage Drive labels](https://support.google.com/a/answer/9292382). \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * Get a label by name.\n * @param {string} labelName The label name.\n */\nfunction getLabel(labelName) {\n try {\n const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});\n const title = label.properties.title;\n const fieldsLength = label.fields.length;\n console.log(`Fetched label with title: '${title}' and ${fieldsLength} fields.`);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get label with error %s', err.message);\n }\n}\n```\n\n### List labels for a Drive item\n\nThe following code sample shows how to get a Drive item and list all labels\napplied to that item. \nadvanced/driveLabels.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/driveLabels.gs) \n\n```javascript\n/**\n * List Labels on a Drive Item\n * Fetches a Drive Item and prints all applied values along with their to their\n * human-readable names.\n *\n * @param {string} fileId The Drive File ID\n */\nfunction listLabelsOnDriveItem(fileId) {\n try {\n const appliedLabels = Drive.Files.listLabels(fileId);\n\n console.log('%d label(s) are applied to this file', appliedLabels.labels.length);\n\n appliedLabels.labels.forEach((appliedLabel) =\u003e {\n // Resource name of the label at the applied revision.\n const labelName = 'labels/' + appliedLabel.id + '@' + appliedLabel.revisionId;\n\n console.log('Fetching Label: %s', labelName);\n const label = DriveLabels.Labels.get(labelName, {view: 'LABEL_VIEW_FULL'});\n\n console.log('Label Title: %s', label.properties.title);\n\n Object.keys(appliedLabel.fields).forEach((fieldId) =\u003e {\n const fieldValue = appliedLabel.fields[fieldId];\n const field = label.fields.find((f) =\u003e f.id == fieldId);\n\n console.log(`Field ID: ${field.id}, Display Name: ${field.properties.displayName}`);\n switch (fieldValue.valueType) {\n case 'text':\n console.log('Text: %s', fieldValue.text[0]);\n break;\n case 'integer':\n console.log('Integer: %d', fieldValue.integer[0]);\n break;\n case 'dateString':\n console.log('Date: %s', fieldValue.dateString[0]);\n break;\n case 'user':\n const user = fieldValue.user.map((user) =\u003e {\n return `${user.emailAddress}: ${user.displayName}`;\n }).join(', ');\n console.log(`User: ${user}`);\n break;\n case 'selection':\n const choices = fieldValue.selection.map((choiceId) =\u003e {\n return field.selectionOptions.choices.find((choice) =\u003e choice.id === choiceId);\n });\n const selection = choices.map((choice) =\u003e {\n return `${choice.id}: ${choice.properties.displayName}`;\n }).join(', ');\n console.log(`Selection: ${selection}`);\n break;\n default:\n console.log('Unknown: %s', fieldValue.valueType);\n console.log(fieldValue.value);\n }\n });\n });\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', err.message);\n }\n}\n```"]]