MS OneDrive – Download Library Item
MS OneDrive – Download Library Item
Retrieves item metadata, then downloads its content. If the content body is empty, the helper tries the mapped downloadUrl. It returns binary file content in Apex, not a Salesforce ContentVersion record. Empty content produces an empty result, so this implementation does not return a usable result for a zero-byte file. Missing name and MIME metadata fall back to download.bin and application/octet-stream. Downloading consumes callouts and Apex heap; it is not a streaming transfer.
Action details
| Property | Value |
|---|---|
| Action label | MS OneDrive – Download Library Item |
| Category | CloudTools |
| Apex class and method | DownloadOneDriveLibraryItem.downloadLibraryItem |
| Input type | DownloadOneDriveLibraryItem.DownloadOneDriveLibraryItemInput |
| Output type | List<MSGraphDriveFile> |
| Authorization | OAuth Delegated (Authorization Code) or OAuth Application (Client Credentials). |
| Flow automation | Use an application connector configured as the Flow default. |
| Microsoft Graph operation | GET /v1.0/drives/{driveId}/items/{itemId}/content for Authorization Code Flow; otherwise GET /v1.0/users/{userIdOrPrincipalName}/drives/{driveId}/items/{itemId}/content. Also reads /items/{itemId} for metadata. |
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.
Delegated calls generally require Files.Read; application calls generally require Files.Read.All. Broader file or site read/write permissions may also satisfy the operation. 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 | Yes | Library (drive) identifier. |
| Item Id | itemId | String | Yes | Library (drive) item identifier. |
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 MSGraphDriveFile with MIME type, file name, and Blob content, or an empty list when no usable content is returned. In Apex, use the Blob to create a Salesforce file or encode it as needed; the action does not save a Salesforce file automatically.
| Output | Apex field | Type | Description |
|---|---|---|---|
| Mime Type | mimeType | String | MIME content type of the file. |
| File Name | fileName | String | File name. |
| File | file | Blob | Binary file content. |
Use in Flow
Add MS OneDrive – Download Library Item in Flow Builder. Use a Microsoft 365 application connector configured as Set as default for Flow automations, supply the target user and resource IDs, and handle faults. Delegated connections are not supported for CloudTools Flow automation. This implementation processes only the first request; it is not a bulk operation.
The file content is an Apex Blob rather than a text variable. Use custom Apex when the workflow needs to store or transform the bytes; the LWC controller supplies a separate Base64 representation.
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. The example supplies the user for application access; omit it for an already-authorized delegated connector.
Apptigent.DownloadOneDriveLibraryItem.DownloadOneDriveLibraryItemInput request =
new Apptigent.DownloadOneDriveLibraryItem.DownloadOneDriveLibraryItemInput();
request.userIdOrPrincipalName = 'REPLACE_WITH_MICROSOFT_USER_ID_OR_UPN';
request.driveId = 'REPLACE_WITH_DRIVE_ID';
request.itemId = 'REPLACE_WITH_ITEM_OR_PARENT_ID';
List<Apptigent.MSGraphDriveFile> results =
Apptigent.DownloadOneDriveLibraryItem.downloadLibraryItem(
new List<Apptigent.DownloadOneDriveLibraryItem.DownloadOneDriveLibraryItemInput>{ request }
);
Blob fileContent = results.isEmpty() ? null : results[0].file;
// Store or transform fileContent in the calling application.
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 downloadOneDriveLibraryItem from msOneDriveConnectorLib, through MSOneDriveConnectorController. These repository c/ imports apply to an LWC in the same namespace. Complete connector authorization before use.
Connector selection limitation: The wrapper accepts a second connectorId argument, but the current controller/action path does not forward it to select the Graph connector for this operation. Calls use CloudTools default connector selection. Passing a connectorId does not guarantee that this action uses that record.
oneDriveActionExample.js
import { LightningElement } from 'lwc';
import { downloadOneDriveLibraryItem, 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.basicLibraryItem();
request.userIdOrPrincipalName = 'REPLACE_WITH_MICROSOFT_USER_ID_OR_UPN';
request.driveId = 'REPLACE_WITH_DRIVE_ID';
request.itemId = 'REPLACE_WITH_ITEM_OR_PARENT_ID';
const result = await downloadOneDriveLibraryItem(request);
if (!result?.base64File) {
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 file object with mimeType, fileName, and base64File; it converts the Apex Blob to Base64.
For delegated sign-in, use getOneDriveAuthorizationState, beginOneDriveAuthorization, and createOneDriveOAuthMessageHandler. The example only reports errors. This controller path converts failures to plain Aura-handled messages, so requiresInteractiveAuthorization is not guaranteed. Use the authorization-state helper before calling and handle message-only failures.
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. |