Load Microsoft OneDrive Data into Your Application
Load files from OneDrive Personal or Business into LlamaIndex, with MSAL authentication.
Why it matters
Seamlessly ingest files and documents from both personal and business Microsoft OneDrive accounts into your AI applications. This loader handles recursive traversal and selective file type filtering, simplifying data preparation for AI workflows.
Outcomes
What it gets done
Connect to Microsoft OneDrive Personal and Business accounts.
Recursively download files and folders.
Filter files by MIME type.
Support user and app authentication via MSAL.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-reader-readers-microsoft-onedrive | bash Overview
Microsoft OneDrive Loader
A LlamaIndex reader for OneDrive Personal and Business, with recursive traversal, MIME-type filtering, and two MSAL authentication modes. Use browser auth for Personal or interactive Business access; client-secret auth is Business-only and unattended.
What it does
The Microsoft OneDrive Loader reads files from both OneDrive Personal and OneDrive for Business, supporting recursive traversal and download of subfolders, and the ability to filter files by MIME type. Files or folders are specified by passing a list of file/folder IDs or file/folder paths to load_data.
Subfolder traversal is enabled by default and can be disabled by passing recursive=False. Files can be filtered by MIME type, for example scoping a load to just Word documents via a mime_types list.
Authentication runs through MSAL and supports two modes. User Authentication is browser-based: it requires an app registration in Microsoft Entra ID with a redirect URI set to https://localhost under mobile/native applications, and Files.Read.All delegated permission - this mode needs manual, interactive login and is not suited to CI/CD or unattended background services. App Authentication uses a client ID and client secret for silent, non-interactive authentication, but Microsoft does not currently support this mode for OneDrive Personal - it works only for OneDrive for Business (Microsoft 365) - and it requires Files.Read.All and User.Read.All application permissions with admin consent granted.
For OneDrive Personal, OneDriveReader can be initialized with just a client_id and interactive login, since app authentication isn't available there. A folder_id or file_id can be extracted directly from a OneDrive URL - the source shows the exact substring pattern to pull from a folder or file preview link - and passed to load_data to scope the load, alongside the default recursive=True behavior for subfolder traversal. For OneDrive for Business, an organization instead initializes the reader with a tenant_id plus a registered client_id and client_secret, and calls load_data with a userprincipalname (typically the user's organizational email) to scope which user's files are loaded.
When to use - and when NOT to
Use browser-based User Authentication for OneDrive Personal or for interactive, human-present scenarios on OneDrive for Business. Use client-secret App Authentication only for OneDrive for Business unattended scenarios - it is explicitly unsupported by Microsoft for OneDrive Personal. Use mime_types filtering when you only need specific file formats rather than everything in scope. Do not expect App Authentication to work against a personal OneDrive account; Microsoft's own platform limitation makes that combination non-functional regardless of configuration.
Capabilities
Recursively loads files from OneDrive Personal or Business by folder ID, file ID, or (for Business) tenant plus user principal name, with optional MIME-type filtering and a choice of browser-based or client-secret MSAL authentication.
How to install
pip install llama-index-readers-microsoft-onedrive
Requires a Microsoft Entra ID app registration; App Authentication additionally requires a client secret and is Business-only.
Who it's for
Developers who need files from a user's or organization's OneDrive loaded into LlamaIndex, whether interactively (Personal or Business) or unattended (Business only).
Source README
Microsoft OneDrive Loader
pip install llama-index-readers-microsoft-onedrive
This loader reads files from:
- Microsoft OneDrive Personal (https://onedrive.live.com/) and
- Microsoft OneDrive for Business (https://portal.office.com/onedrive).
It supports recursively traversing and downloading files from subfolders and provides capability to download only files with specific mime types. To use this loader, you need to pass in a list of file/folder id or file/folder paths.
Subfolder traversing (enabled by default)
To disable: loader.load_data(recursive = False)
Mime types
You can also filter the files by the mimeType e.g.: mime_types=["application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
Authentication
OneDriveReader supports following two MSAL authentication:
1. User Authentication: Browser based authentication:
- You need to create a app registration in Microsoft Entra (formerly Azure Active Directory)
- For interactive authentication to work, a browser is used to authenticate, hence the registered application should have a redirect URI set to 'https://localhost' under mobile and native applications.
- This mode of authentication is not suitable for CI/CD or other background service scenarios where manual authentication isn't feasible.
- API Permission required for registered app:
Microsoft Graph --> Delegated Permission -- > Files.Read.All
2. App Authentication: Client ID & Client Secret based authentication
You need to create a app registration in Microsoft Entra (formerly Azure Active Directory)
For silent authentication to work, You need to create a client secret as well for the app.
This mode of authnetication is not supported by Microsoft currently for OneDrive Personal, hence this can be used only for OneDrive for Business(Microsoft 365).
API Permission required for registered app:
Microsoft Graph --> Application Permissions -- > Files.Read.All (Grant Admin Consent)
Microsoft Graph --> Application Permissions -- > User.Read.All (Grant Admin Consent)
Usage
OneDrive Personal
Note: If you trying to connect to OneDrive Personal you can initialize OneDriveReader with just your clientid and interactive login. Microsoft _doesn't support App authentication for OneDrive Personal currently.
folder_id
You can extract a folder_id directly from its URL.
For example, the folder_id of https://onedrive.live.com/?id=B5AF52B769DFDE4%216107&cid=0B5AF52B769DFDdRE4 is B5AF52B769DFDE4%216107.
file_id
You can extract a file_id directly from its preview URL.
For example, the file_id of https://onedrive.live.com/?cid=0B5AF52BE769DFDE4&id=B5AF52B769DFDE4%216106&parId=root&o=OneUp is B5AF52B769DFDE4%216106.
OneDrive Personal Example Usage:
from llama_index.readers.microsoft_onedrive import OneDriveReader
### User Authentication flow: Replace client id with your own id
loader = OneDriveReader(client_id="82ee706e-2439-47fa-877a-95048ead9318")
### APP Authentication flow: NOT SUPPORTED By Microsoft
#### Get all documents including subfolders.
documents = loader.load_data()
#### Get documents using folder_id , to exclude traversing subfolders explicitly set the recursive flag to False, default is True
documents = loader.load_data(folder_id="folderid", recursive=False)
#### Using file ids
documents = loader.load_data(file_ids=["fileid1", "fileid2"])
OneDrive For Business
https://portal.office.com/onedrive
Note: If you are an organization trying to connect to OneDrive for Business (Part of Microsoft 365), you need to:
- Initialize OneDriveReader with correct tenant_id, along with a client_id and client_Secret registered for the tenant.
- Invoke the load_data method with userprincipalname (org provided email in most cases)
folder_path
The relative pathof subf
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.