MS OneDrive – Get Library
MS OneDrive – Get Library
Retrieves the default drive when Library Id is blank, or the specified drive when it is populated. Microsoft currently documents Get Drive as supporting delegated permissions only; do not plan this action as application-based Flow automation. Get Libraries and operations on a known drive have their own permission requirements. The action provides no automatic fallback from Get Drive to another endpoint.
Action details
| Property | Value |
|---|---|
| Action label | MS OneDrive – Get Library |
| Category | CloudTools |
| Apex class and method | GetOneDriveLibrary.getLibrary |
| Input type | GetOneDriveLibrary.GetOneDriveLibraryInput |
| Output type | List<MSGraphDrive> |
| Authorization | OAuth Delegated (Authorization Code). |
| Flow automation | Not supported; the Get Drive API requires delegated access. |
| Microsoft Graph operation | GET /v1.0/me/drive when delegated and Library Id is blank, or GET /v1.0/drives/{driveId} when provided. The implementation also builds /users/{userIdOrPrincipalName}/drive or /users/{userIdOrPrincipalName}/drives/{driveId} for other authorization types; those branches do not make application access supported. |
Prerequisites and permissions
Configure a CloudTools Connector with Connector Type set to Microsoft 365. For delegated access, complete the current user’s Microsoft sign-in. For application access where supported, grant application permissions and administrator consent and provide the target user ID or principal name. The identity must have access to the selected drive and item.
Microsoft lists Files.Read as the least privileged delegated permission. Application permissions are not supported for Get Drive. See the Microsoft operation and permissions reference.
Drive selection: For Authorization Code Flow, a populated driveId uses /drives/{driveId} and userIdOrPrincipalName is ignored. Other authorization types require userIdOrPrincipalName and use the user-scoped drive path shown above. A Library Id is a Graph drive ID and Item Id is an item within that drive; neither is a Salesforce record ID or browser URL.
Inputs
The table lists action labels, Apex field names, and Apex data types. Supply one non-null request with nonblank required text values.
| Input | Apex field | Type | Required | Description |
|---|---|---|---|---|
| User Id or Principal Name | userIdOrPrincipalName | String | Unless Authorization Code Flow is enabled | User id or user principal name. Ignored for Authorization Code Flow. |
| Library Id | driveId | String | No | Library (drive) identifier. Leave blank for the default drive.. |
| Select | selectQuery | String | No | Comma-separated properties to return. Supply only the OData value, without $select=, $top=, $orderby=, or a full URL. Microsoft validates support. |
The global invocable input has no connectorId field. Direct subscriber Apex and Flow calls use CloudTools connector selection. This action does not resolve replacement tokens; resolve dynamic values before invocation. See Using Token Replacements.
Outputs
Returns one MSGraphDrive inside a list, or an empty list when no response is mapped. Check for an empty result and a populated resource ID before subsequent operations.
| Output | Apex field | Type | Description |
|---|---|---|---|
| Id | id | String | Unique identifier for the library (drive). |
| Name | name | String | Name of the library (drive). |
| Library Type | driveType | String | Type of library (drive). |
| Web Url | webUrl | String | Web URL for the library (drive). |
| Created Date Time | createdDateTime | String | Created date time for the library (drive). |
| Last Modified Date Time | lastModifiedDateTime | String | Last modified date time for the library (drive). |
| OData Type | odataType | String | OData type for the library (drive). |
| Additional Properties | additionalProperties | List<DynamicObject> | Additional properties from user-defined query. |
Use in Flow
Add MS OneDrive – Get Library in Flow Builder. This action appears in Flow Builder but is not supported for CloudTools Flow automation because Get Drive requires delegated access. Use an interactive experience with Microsoft authorization. A Flow-default application connector or Named Credential does not remove that API requirement.
Use in custom Apex
This subscriber-org example uses the Apptigent namespace. Replace all identifiers and run in a context that permits callouts with a configured connector. Authorize the delegated Microsoft user first; the example omits userIdOrPrincipalName.
Apptigent.GetOneDriveLibrary.GetOneDriveLibraryInput request =
new Apptigent.GetOneDriveLibrary.GetOneDriveLibraryInput();
List<Apptigent.MSGraphDrive> results =
Apptigent.GetOneDriveLibrary.getLibrary(
new List<Apptigent.GetOneDriveLibrary.GetOneDriveLibraryInput>{ request }
);
Apptigent.MSGraphDrive item = results.isEmpty() ? null : results[0];
String resultId = item == null ? null : item.id;
System.debug(resultId);
The example leaves exception handling, authorization setup, and presentation or persistence of the result to the calling application.
Use in a Lightning Web Component
Use getOneDriveLibrary from msOneDriveConnectorLib, through MSOneDriveConnectorController. These repository c/ imports apply to an LWC in the same namespace. Complete connector authorization before use.
Pass the selected Connector record ID as the second JavaScript argument. The controller forwards it through the action’s internal overload. The public internal Apex overload is not the global subscriber API shown above.
oneDriveActionExample.js
import { LightningElement } from 'lwc';
import { getOneDriveLibrary, MSOneDriveObjects } from 'c/msOneDriveConnectorLib';
export default class OneDriveActionExample extends LightningElement {
connectorId = 'REPLACE_WITH_CONNECTOR_RECORD_ID';
result = null;
errorMessage = '';
isRunning = false;
completed = false;
async handleRun() {
if (this.isRunning) {
return;
}
this.isRunning = true;
this.errorMessage = '';
try {
const request = MSOneDriveObjects.inputs.getOneDriveLibrary();
const result = await getOneDriveLibrary(request, this.connectorId);
if (!result?.id) {
throw new Error('The action did not return a usable result.');
}
this.result = result;
this.completed = true;
} catch (error) {
this.errorMessage = error.requiresInteractiveAuthorization
? 'Sign in to Microsoft for the connector, then try again.'
: error.message || 'The OneDrive action failed.';
} finally {
this.isRunning = false;
}
}
}
oneDriveActionExample.html
<template>
<lightning-button label="Run action" onclick={handleRun}
disabled={isRunning}></lightning-button>
<p role="alert">{errorMessage}</p>
</template>
The library resolves to one resource object or null when no response is mapped; read result.id directly.
The controller adds rawJson as a serialization of the mapped Apex object. This does not preserve Microsoft fields omitted from the model and is not an invocable Flow output.
For structured failures, getOneDriveLibraryResult returns success, code, message, requiresInteractiveAuthorization, connector metadata, resultJson, and a parsed payload. The convenience function used above unwraps payload on success and throws on failure. The Result wrapper checks authorization before calling the action; it does not sign the user in.
For delegated sign-in, use getOneDriveAuthorizationState, beginOneDriveAuthorization, and createOneDriveOAuthMessageHandler. The example only reports errors. This controller path exposes structured requiresInteractiveAuthorization errors.
Error handling and troubleshooting
| Symptom | What to check |
|---|---|
| Required fields error | Provide a nonempty request list with a non-null first item and the required identifiers. Supply the user for non-delegated connectors. |
| Authorization or access denied | Check connector selection, Microsoft sign-in, consent, permission type, and access to the drive or item. |
| Resource not found | Verify driveId, itemId, and any version or permission ID. Keep related IDs associated with their source drive. |
| Missing fields or items | Check Select, item type, mapping limitations, and first-page behavior. Empty results and omitted fields are not proof that a resource does not exist. |
| Unexpected connector | Review the connector selection limitation above and the Microsoft 365 Flow default. |
| Operation rejected | Inspect the Microsoft error, item state, library capability, and tenant policy. Handle exceptions or structured Result failures in the caller. |