Skip to main content

Actions

The ballerinax/microsoft.onedrive package exposes the following clients:

ClientPurpose
ClientMicrosoft Graph v1.0 API: drive and item CRUD, content management, sharing, search, and actions.

Client

Microsoft Graph v1.0 API: drive and item CRUD, content management, sharing, search, and actions.

Configuration

FieldTypeDefaultDescription
authOAuth2RefreshTokenGrantConfig|BearerTokenConfigRequiredOAuth 2.0 refresh token configuration or bearer token. The refresh URL defaults to https://login.microsoftonline.com/common/oauth2/v2.0/token.
httpVersionHttpVersionHTTP_2_0HTTP protocol version.
timeoutdecimal60Request timeout in seconds.
retryConfigRetryConfig()Retry configuration for failed requests.
secureSocketClientSecureSocket()SSL/TLS configuration.
proxyProxyConfig()Proxy server configuration.

Initializing the client

import ballerinax/microsoft.onedrive;

configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;

onedrive:Client oneDrive = check new (
config = {
auth: {
clientId: clientId,
clientSecret: clientSecret,
refreshToken: refreshToken,
scopes: ["Files.Read", "Files.Read.All", "Files.ReadWrite", "Files.ReadWrite.All"]
}
}
);

Operations

Drive management

listDrive

Retrieves the list of drives available to the authenticated user.

Parameters:

NameTypeRequiredDescription
headersmap<string|string[]>NoOptional HTTP headers.
queriesListDriveQueriesNoOData query parameters ($select, $filter, $top, $skip, $orderby, $expand, $count).

Returns: DriveCollectionResponse|error

Sample code:

onedrive:DriveCollectionResponse drives = check oneDrive->listDrive();

Sample response:

{
"value": [
{
"id": "b!xGz3a2VHOkqJRsBv0AAAA",
"name": "OneDrive",
"driveType": "personal",
"owner": {
"user": {
"displayName": "John Doe",
"id": "48d31887-5fad-4d73-a9f5-3c356e68a038"
}
},
"quota": {
"total": 5368709120,
"used": 1073741824,
"remaining": 4294967296
}
}
]
}
getDrive

Retrieves the properties and relationships of a specific drive by ID.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetDriveQueriesNoOData query parameters ($select, $expand).

Returns: Drive|error

Sample code:

onedrive:Drive drive = check oneDrive->getDrive("b!xGz3a2VHOkqJRsBv0AAAA");

Sample response:

{
"id": "b!xGz3a2VHOkqJRsBv0AAAA",
"name": "OneDrive",
"driveType": "personal",
"owner": {
"user": {
"displayName": "John Doe"
}
}
}
createDrive

Creates a new drive with the specified properties.

Parameters:

NameTypeRequiredDescription
payloadDriveYesThe drive resource to create.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Drive|error

Sample code:

onedrive:Drive newDrive = check oneDrive->createDrive({
name: "Project Files",
driveType: "personal"
});

Sample response:

{
"id": "b!yHz4b3WHPkrKSsBw1BBBB",
"name": "Project Files",
"driveType": "personal"
}
updateDrive

Updates the properties of a drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
payloadDriveYesThe drive properties to update.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Drive|error

Sample code:

onedrive:Drive updated = check oneDrive->updateDrive("b!xGz3a2VHOkqJRsBv0AAAA", {
name: "Updated Drive Name"
});

Sample response:

{
"id": "b!xGz3a2VHOkqJRsBv0AAAA",
"name": "Updated Drive Name",
"driveType": "personal"
}
deleteDrive

Deletes a drive by its ID.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersDeleteDriveHeadersNoOptional HTTP headers including If-Match.

Returns: error?

Sample code:

check oneDrive->deleteDrive("b!xGz3a2VHOkqJRsBv0AAAA");

Item CRUD

listItem

Lists the drive items (files and folders) in a drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesListItemQueriesNoOData query parameters.

Returns: DriveItemCollectionResponse|error

Sample code:

onedrive:DriveItemCollectionResponse items = check oneDrive->listItem(driveId);

Sample response:

{
"value": [
{
"id": "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD36K",
"name": "Documents",
"folder": {"childCount": 5},
"size": 0
},
{
"id": "01BYE5RZ4YAEGUIA3BZZC3JM5IWSZWDVBP",
"name": "report.pdf",
"file": {"mimeType": "application/pdf"},
"size": 245890
}
]
}
getItem

Retrieves the metadata for a drive item by its ID.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetItemQueriesNoOData query parameters.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem item = check oneDrive->getItem(driveId, "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD36K");

Sample response:

{
"id": "01BYE5RZ6QN3ZWBTUFOFD3GSPGOHDJD36K",
"name": "Documents",
"folder": {"childCount": 5},
"size": 0,
"createdDateTime": "2024-01-15T10:30:00Z",
"lastModifiedDateTime": "2024-06-20T14:22:00Z"
}
createItem

Creates a new drive item in the specified drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
payloadDriveItemYesThe drive item resource to create.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem newItem = check oneDrive->createItem(driveId, {
name: "NewFolder",
folder: {}
});

Sample response:

{
"id": "01BYE5RZ7XHKK5MN2BOVAL2MCSITPQHBWP",
"name": "NewFolder",
"folder": {"childCount": 0},
"size": 0
}
updateItem

Updates the metadata of an existing drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemYesThe updated drive item properties.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem updated = check oneDrive->updateItem(driveId, driveItemId, {
name: "RenamedFolder"
});

Sample response:

{
"id": "01BYE5RZ7XHKK5MN2BOVAL2MCSITPQHBWP",
"name": "RenamedFolder",
"folder": {"childCount": 0}
}
deleteItem

Deletes a drive item (file or folder) by its ID.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersDeleteItemHeadersNoOptional HTTP headers including If-Match.

Returns: error?

Sample code:

check oneDrive->deleteItem(driveId, "01BYE5RZ7XHKK5MN2BOVAL2MCSITPQHBWP");

Item content

getItemsContent

Downloads the content (bytes) of a drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetItemsContentQueriesNoOData query parameters.

Returns: byte[]|error

Sample code:

byte[] content = check oneDrive->getItemsContent(driveId, driveItemId);

Sample response:

[80, 75, 3, 4, 20, 0, 6, ...]
setItemsContent

Uploads or replaces the content of a drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadbyte[]YesThe file content as a byte array.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

byte[] fileContent = check io:fileReadBytes("files/document.pdf");
onedrive:DriveItem item = check oneDrive->setItemsContent(driveId, driveItemId, fileContent);

Sample response:

{
"id": "01BYE5RZ4YAEGUIA3BZZC3JM5IWSZWDVBP",
"name": "document.pdf",
"size": 245890,
"file": {"mimeType": "application/pdf"}
}
deleteItemsContent

Deletes the content stream of a drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersDeleteItemsContentHeadersNoOptional HTTP headers.

Returns: error?

Sample code:

check oneDrive->deleteItemsContent(driveId, driveItemId);

Children management

listChildren

Lists the child items (files and folders) of a drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the parent drive item.
headersmap<string|string[]>NoOptional HTTP headers.
queriesListChildrenQueriesNoOData query parameters.

Returns: DriveItemCollectionResponse|error

Sample code:

onedrive:DriveItemCollectionResponse children = check oneDrive->listChildren(driveId, folderId);

Sample response:

{
"value": [
{"id": "01BYE5RZ111", "name": "photo.jpg", "file": {"mimeType": "image/jpeg"}, "size": 512000},
{"id": "01BYE5RZ222", "name": "notes.txt", "file": {"mimeType": "text/plain"}, "size": 1024}
]
}
createChildren

Creates a new child item (file or folder) under the specified parent item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the parent drive item.
payloadDriveItemYesThe child drive item to create.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem folder = check oneDrive->createChildren(driveId, parentId, {
name: "Reports",
folder: {}
});

Sample response:

{
"id": "01BYE5RZ333",
"name": "Reports",
"folder": {"childCount": 0}
}
createChildrenInRoot

Creates a new child item under the root folder of the drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
payloadDriveItemYesThe child drive item to create.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem folder = check oneDrive->createChildrenInRoot(driveId, {
name: "Upload",
folder: {}
});

Sample response:

{
"id": "01BYE5RZ444",
"name": "Upload",
"folder": {"childCount": 0}
}
setChildrenContentByPath

Uploads file content to a specific path relative to the drive root.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
pathstringYesThe path relative to the drive root (e.g., /Upload/document.pdf).
payloadbyte[]YesThe file content as a byte array.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

byte[] fileContent = check io:fileReadBytes("files/github.png");
onedrive:DriveItem item = check oneDrive->setChildrenContentByPath(
driveId, "/Upload/github.png", fileContent
);

Sample response:

{
"id": "01BYE5RZ555",
"name": "github.png",
"size": 34567,
"file": {"mimeType": "image/png"}
}
getChildrenContentByPath

Downloads content of a file at a specific path relative to the drive root.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
pathstringYesThe path relative to the drive root.
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetChildrenContentByPathQueriesNoOData query parameters.

Returns: byte[]|error

Sample code:

byte[] content = check oneDrive->getChildrenContentByPath(driveId, "/Documents/report.pdf");

Sample response:

[37, 80, 68, 70, 45, 49, 46, ...]

Root operations

getRoot

Retrieves the root folder of a drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetRootQueriesNoOData query parameters.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem root = check oneDrive->getRoot(driveId);

Sample response:

{
"id": "01BYE5RZROOT",
"name": "root",
"folder": {"childCount": 12},
"root": {},
"size": 1073741824
}
listChildrenInRoot

Lists children of the root folder of a drive.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesListChildrenInRootQueriesNoOData query parameters.

Returns: DriveItemCollectionResponse|error

Sample code:

onedrive:DriveItemCollectionResponse rootItems = check oneDrive->listChildrenInRoot(driveId);

Sample response:

{
"value": [
{"id": "01BYE5RZ001", "name": "Documents", "folder": {"childCount": 5}},
{"id": "01BYE5RZ002", "name": "Pictures", "folder": {"childCount": 23}}
]
}
getItemByPath

Retrieves a drive item by its path relative to the drive root.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
pathstringYesThe item path relative to the drive root (e.g., /Documents/report.pdf).
headersmap<string|string[]>NoOptional HTTP headers.
queriesGetItemByPathQueriesNoOData query parameters.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem item = check oneDrive->getItemByPath(driveId, "/Documents/report.pdf");

Sample response:

{
"id": "01BYE5RZ4YAEGUIA3BZZC3JM5IWSZWDVBP",
"name": "report.pdf",
"size": 245890,
"file": {"mimeType": "application/pdf"},
"parentReference": {
"driveId": "b!xGz3a2VHOkqJRsBv0AAAA",
"path": "/drive/root:/Documents"
}
}

Search & discovery

search

Searches for drive items matching a query string within a specific item's hierarchy.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe drive item ID to search within.
qstring?NoThe search query text.
queriesSearchQueriesNoOData query parameters.

Returns: CollectionOfDriveItem|error

Sample code:

onedrive:CollectionOfDriveItem results = check oneDrive->search(driveId, driveItemId, q = "quarterly report");

Sample response:

{
"value": [
{"id": "01BYE5RZ777", "name": "Q1 Quarterly Report.docx", "size": 56000},
{"id": "01BYE5RZ888", "name": "Q2 Quarterly Report.pdf", "size": 78000}
]
}
recent

Lists recently accessed drive items for the authenticated user.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesRecentQueriesNoOData query parameters.

Returns: CollectionOfDriveItem_1|error

Sample code:

onedrive:CollectionOfDriveItem_1 recentItems = check oneDrive->recent(driveId);

Sample response:

{
"value": [
{"id": "01BYE5RZ999", "name": "presentation.pptx", "size": 1200000, "lastModifiedDateTime": "2024-06-20T14:22:00Z"},
{"id": "01BYE5RZAAA", "name": "budget.xlsx", "size": 45000, "lastModifiedDateTime": "2024-06-19T09:15:00Z"}
]
}
sharedWithMe

Lists drive items that have been shared with the authenticated user.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
headersmap<string|string[]>NoOptional HTTP headers.
queriesSharedWithMeQueriesNoOData query parameters.

Returns: CollectionOfDriveItem_1|error

Sample code:

onedrive:CollectionOfDriveItem_1 shared = check oneDrive->sharedWithMe(driveId);

Sample response:

{
"value": [
{
"id": "01BYE5RZBBB",
"name": "SharedDoc.docx",
"size": 32000,
"shared": {"owner": {"user": {"displayName": "Jane Smith"}}}
}
]
}

Sharing & permissions

createLink

Creates a sharing link for a drive item.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphCreateLinkBodyYesLink creation parameters including type, scope, password, and expiration.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: Permission|error

Sample code:

onedrive:Permission link = check oneDrive->createLink(driveId, driveItemId, {
'type: "view",
scope: "anonymous"
});

Sample response:

{
"id": "1234abcd-5678-efgh",
"roles": ["read"],
"link": {
"type": "view",
"scope": "anonymous",
"webUrl": "https://1drv.ms/u/s!AmRYJXO2laUGxxxxx"
}
}
invite

Sends a sharing invitation for a drive item to specified recipients.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphInviteBodyYesInvitation parameters including recipients, roles, and message.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: CollectionOfPermission|error

Sample code:

onedrive:CollectionOfPermission perms = check oneDrive->invite(driveId, driveItemId, {
recipients: [{email: "[email protected]"}],
roles: ["write"],
requireSignIn: true,
sendInvitation: true,
message: "Please review this document"
});

Sample response:

{
"value": [
{
"id": "perm-5678",
"roles": ["write"],
"grantedTo": {
"user": {"email": "[email protected]", "displayName": "Colleague"}
}
}
]
}

Item actions

copy

Creates a copy of a drive item in a new location.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item to copy.
payloadDriveItemIdMicrosoftGraphCopyBodyYesCopy parameters including destination parentReference and optional new name.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem copied = check oneDrive->copy(driveId, driveItemId, {
name: "report-copy.pdf",
parentReference: {driveId: driveId, id: targetFolderId}
});

Sample response:

{
"id": "01BYE5RZCCC",
"name": "report-copy.pdf",
"size": 245890,
"file": {"mimeType": "application/pdf"}
}
checkin

Checks in a checked-out drive item, making it available to others.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphCheckinBodyYesCheck-in parameters including optional comment.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check oneDrive->checkin(driveId, driveItemId, {
comment: "Updated Q1 figures"
});
checkout

Checks out a drive item, preventing others from editing it.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check oneDrive->checkout(driveId, driveItemId);
createUploadSession

Creates an upload session for uploading large files in byte ranges.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphCreateUploadSessionBodyYesUpload session creation parameters.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: UploadSession|error

Sample code:

onedrive:UploadSession session = check oneDrive->createUploadSession(driveId, driveItemId, {});

Sample response:

{
"uploadUrl": "https://sn3302.up.1drv.com/up/fe6987415ace7X4811...",
"expirationDateTime": "2024-06-25T14:00:00Z",
"nextExpectedRanges": ["0-"]
}
follow

Follows a drive item to receive notifications about changes.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem followed = check oneDrive->follow(driveId, driveItemId);

Sample response:

{
"id": "01BYE5RZ4YAEGUIA3BZZC3JM5IWSZWDVBP",
"name": "report.pdf",
"size": 245890
}
unfollow

Unfollows a drive item to stop receiving change notifications.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check oneDrive->unfollow(driveId, driveItemId);
permanentDelete

Permanently deletes a drive item, bypassing the recycle bin.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: error?

Sample code:

check oneDrive->permanentDelete(driveId, driveItemId);
restore

Restores a drive item that was previously deleted.

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphRestoreBodyYesRestore parameters including optional parentReference and name.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: DriveItem|error

Sample code:

onedrive:DriveItem restored = check oneDrive->restore(driveId, driveItemId, {});

Sample response:

{
"id": "01BYE5RZ4YAEGUIA3BZZC3JM5IWSZWDVBP",
"name": "report.pdf",
"size": 245890
}
preview

Generates a preview URL for a drive item (embeddable viewer).

Parameters:

NameTypeRequiredDescription
driveIdstringYesThe unique identifier of the drive.
driveItemIdstringYesThe unique identifier of the drive item.
payloadDriveItemIdMicrosoftGraphPreviewBodyYesPreview parameters including optional page number and zoom level.
headersmap<string|string[]>NoOptional HTTP headers.

Returns: ItemPreviewInfo|error

Sample code:

onedrive:ItemPreviewInfo preview = check oneDrive->preview(driveId, driveItemId, {});

Sample response:

{
"getUrl": "https://onedrive.live.com/embed?cid=ABCDEF&resid=ABCDEF!123&authkey=xxxxx",
"postParameters": "",
"postUrl": ""
}