Files (OneDrive & SharePoint) - Microsoft Graph API
This resource covers all endpoints related to file operations, OneDrive, SharePoint sites, drives, and document management.
Contents
- Base Endpoints
- Drives
- Drive Items (Files & Folders)
- Upload Files
- Download Files
- Create Folders
- Update Items
- Move and Copy
- Delete Items
- Search
- Sharing
- Special Folders
- Thumbnails
- Delta Queries (Track Changes)
- Versions
- SharePoint Sites
- SharePoint Lists
- Item Properties
- Permissions Reference
- Common Patterns
- Best Practices
- Rate Limits
- Error Handling
Base Endpoints
- User's Drive:
https://graph.microsoft.com/v1.0/me/drive - Specific Drive:
https://graph.microsoft.com/v1.0/drives/{drive-id} - SharePoint Site:
https://graph.microsoft.com/v1.0/sites/{site-id} - Group Drive:
https://graph.microsoft.com/v1.0/groups/{group-id}/drive
Drives
Get User's Default Drive
GET /me/drive
Get Specific Drive
GET /drives/{drive-id}
List User's Drives
GET /me/drives
Get Group Drive
GET /groups/{group-id}/drive
Get Site Drive
GET /sites/{site-id}/drive
Drive properties:
id- Drive IDdriveType- personal, business, documentLibraryowner- Drive ownerquota- Storage quota information
---
Drive Items (Files & Folders)
Get Root Folder
GET /me/drive/root
GET /me/drive/root/children
Get Item by ID
GET /me/drive/items/{item-id}
Get Item by Path
GET /me/drive/root:/Documents/report.pdf
GET /me/drive/root:/Documents/report.pdf:/content
List Folder Contents
GET /me/drive/items/{folder-id}/children
GET /me/drive/root:/Documents:/children
Query Parameters
# Select specific properties
GET /me/drive/root/children?$select=name,size,lastModifiedDateTime
# Order by name
GET /me/drive/root/children?$orderby=name
# Filter by type
GET /me/drive/root/children?$filter=file ne null
# Expand thumbnails
GET /me/drive/root/children?$expand=thumbnails
---
Upload Files
Simple Upload (< 4 MB)
PUT /me/drive/root:/Documents/newfile.txt:/content
Content-Type: text/plain
File content here
Upload Binary File
PUT /me/drive/root:/Documents/image.jpg:/content
Content-Type: image/jpeg
[Binary file content]
Upload to Specific Folder
PUT /me/drive/items/{folder-id}:/{filename}:/content
Content-Type: application/octet-stream
[File content]
Large File Upload (> 4 MB)
Use upload sessions for files > 4 MB:
1. Create Upload Session
POST /me/drive/root:/Documents/largefile.zip:/createUploadSession
Content-Type: application/json
{
"item": {
"@microsoft.graph.conflictBehavior": "rename",
"name": "largefile.zip"
}
}
Response includes uploadUrl
2. Upload Bytes in Fragments
PUT {uploadUrl}
Content-Range: bytes 0-999999/10000000
Content-Type: application/octet-stream
[First 1 MB chunk]
3. Continue Until Complete
PUT {uploadUrl}
Content-Range: bytes 1000000-1999999/10000000
[Next 1 MB chunk]
Recommendations:
- Fragment size: 5-10 MB
- Upload fragments sequentially
- Handle resume on network failures
- Check upload status with GET to uploadUrl
---
Download Files
Download File Content
GET /me/drive/items/{item-id}/content
Returns file binary content with redirect.
Download File by Path
GET /me/drive/root:/Documents/report.pdf:/content
Get Download URL
GET /me/drive/items/{item-id}?$select=@microsoft.graph.downloadUrl
downloadUrl is short-lived (few minutes), redirect to actual content.
Download Specific Format (Office files)
GET /me/drive/items/{item-id}/content?format=pdf
Supported formats: pdf, html (for Office documents)
---
Create Folders
Create Folder
POST /me/drive/root/children
Content-Type: application/json
{
"name": "New Folder",
"folder": {}
}
Create Nested Folder
POST /me/drive/root:/Documents:/children
Content-Type: application/json
{
"name": "Subfolder",
"folder": {},
"@microsoft.graph.conflictBehavior": "rename"
}
Conflict behaviors:
rename- Rename if existsreplace- Replace if existsfail- Fail if exists (default)
---
Update Items
Rename File or Folder
PATCH /me/drive/items/{item-id}
Content-Type: application/json
{
"name": "NewFileName.txt"
}
Update Metadata
PATCH /me/drive/items/{item-id}
Content-Type: application/json
{
"name": "UpdatedName.txt",
"description": "File description"
}
---
Move and Copy
Move Item
PATCH /me/drive/items/{item-id}
Content-Type: application/json
{
"parentReference": {
"id": "{destination-folder-id}"
}
}
Move and Rename
PATCH /me/drive/items/{item-id}
{
"parentReference": {
"id": "{destination-folder-id}"
},
"name": "NewName.txt"
}
Copy Item
POST /me/drive/items/{item-id}/copy
Content-Type: application/json
{
"parentReference": {
"id": "{destination-folder-id}"
},
"name": "Copy of file.txt"
}
Copy is asynchronous - returns Location header with monitor URL.
Monitor Copy Progress
GET {monitor-url}
---
Delete Items
Delete File or Folder
DELETE /me/drive/items/{item-id}
Moves to Recycle Bin (if available).
Permanent Delete
DELETE /me/drive/items/{item-id}?@microsoft.graph.permanentDelete=true
---
Search
Search in Drive
GET /me/drive/root/search(q='{search-query}')
GET /me/drive/root/search(q='report')?$select=name,size,webUrl
Search in Specific Folder
GET /me/drive/items/{folder-id}/search(q='{query}')
Search supports:
- File names
- File content (when indexed)
- Metadata
---
Sharing
Create Sharing Link
POST /me/drive/items/{item-id}/createLink
Content-Type: application/json
{
"type": "view",
"scope": "anonymous"
}
Link types:
view- Read-onlyedit- Read and writeembed- Embeddable link
Scopes:
anonymous- Anyone with the linkorganization- Anyone in your organizationusers- Specific users (requires recipients)
Create Link with Password
POST /me/drive/items/{item-id}/createLink
{
"type": "view",
"scope": "anonymous",
"password": "securepassword",
"expirationDateTime": "2024-12-31T23:59:59Z"
}
Send Sharing Invitation
POST /me/drive/items/{item-id}/invite
Content-Type: application/json
{
"requireSignIn": true,
"sendInvitation": true,
"roles": ["read"],
"recipients": [
{"email": "user@example.com"}
],
"message": "Here's the file you requested."
}
Roles: read, write, owner
List Permissions
GET /me/drive/items/{item-id}/permissions
Remove Permission
DELETE /me/drive/items/{item-id}/permissions/{permission-id}
---
Special Folders
Access Special Folders
GET /me/drive/special/{folder-name}
Special folder names:
documents- Documentsphotos- PhotoscameraRoll- Camera Rollapproot- App foldermusic- Musicdownloads- Downloads
List Files in Special Folder
GET /me/drive/special/documents/children
---
Thumbnails
Get Thumbnails
GET /me/drive/items/{item-id}/thumbnails
Returns multiple sizes:
small- 96x96medium- 176x176large- 800x800 (largest dimension)
Get Specific Thumbnail Size
GET /me/drive/items/{item-id}/thumbnails/0/medium/content
Custom Thumbnail Size
GET /me/drive/items/{item-id}/thumbnails/0/c400x400/content
Format: c{width}x{height} for custom size
---
Delta Queries (Track Changes)
Initial Delta Request
GET /me/drive/root/delta
Returns:
- Changed items
@odata.deltaLinkfor next query
Subsequent Delta Requests
GET {deltaLink}
Use cases:
- Sync files
- Track changes
- Update local cache
---
Versions
List File Versions
GET /me/drive/items/{item-id}/versions
Get Specific Version
GET /me/drive/items/{item-id}/versions/{version-id}
Download Version Content
GET /me/drive/items/{item-id}/versions/{version-id}/content
Restore Version
POST /me/drive/items/{item-id}/versions/{version-id}/restoreVersion
---
SharePoint Sites
Search Sites
GET /sites?search={query}
GET /sites?search=Engineering
Get Root Site
GET /sites/root
Get Site by Path
GET /sites/{hostname}:/{site-path}
GET /sites/contoso.sharepoint.com:/sites/engineering
Get Site by ID
GET /sites/{site-id}
List Site Drives
GET /sites/{site-id}/drives
Get Site Document Library
GET /sites/{site-id}/drive
---
SharePoint Lists
List Site Lists
GET /sites/{site-id}/lists
Get Specific List
GET /sites/{site-id}/lists/{list-id}
List Items
GET /sites/{site-id}/lists/{list-id}/items
GET /sites/{site-id}/lists/{list-id}/items?$expand=fields
Create List Item
POST /sites/{site-id}/lists/{list-id}/items
Content-Type: application/json
{
"fields": {
"Title": "New Item",
"Description": "Item description"
}
}
Update List Item
PATCH /sites/{site-id}/lists/{list-id}/items/{item-id}/fields
Content-Type: application/json
{
"Title": "Updated Title"
}
Delete List Item
DELETE /sites/{site-id}/lists/{list-id}/items/{item-id}
---
Item Properties
Core Properties
id- Item IDname- File/folder namesize- Size in bytescreatedDateTime- Creation timelastModifiedDateTime- Last modified timewebUrl- Web URL to itemparentReference- Parent folder referencefile- File facet (if file)folder- Folder facet (if folder)package- Package facet (if package)image- Image metadata (if image)photo- Photo metadata (if photo)video- Video metadata (if video)
File Facet
{
"file": {
"mimeType": "application/pdf",
"hashes": {
"sha1Hash": "...",
"quickXorHash": "..."
}
}
}
Folder Facet
{
"folder": {
"childCount": 5
}
}
---
Permissions Reference
Delegated Permissions
Files.Read- Read user filesFiles.ReadWrite- Read and write user filesFiles.Read.All- Read all files user can accessFiles.ReadWrite.All- Read and write all files user can accessSites.Read.All- Read items in all site collectionsSites.ReadWrite.All- Read and write items in all site collections
Application Permissions
Files.Read.All- Read files in all site collectionsFiles.ReadWrite.All- Read and write files in all site collectionsSites.Read.All- Read items in all site collectionsSites.ReadWrite.All- Read and write items in all site collections
---
Common Patterns
Upload and Share File
# 1. Upload file
PUT /me/drive/root:/Documents/report.pdf:/content
Content-Type: application/pdf
[File content]
# 2. Create sharing link
POST /me/drive/root:/Documents/report.pdf:/createLink
{
"type": "view",
"scope": "organization"
}
Sync Folder
# 1. Get initial state
GET /me/drive/root:/Documents:/delta
# 2. Process items
# 3. Store deltaLink
# 4. Get changes
GET {deltaLink}
Download All Files in Folder
# 1. List folder contents
GET /me/drive/items/{folder-id}/children
# 2. For each item, download content
GET /me/drive/items/{item-id}/content
---
Best Practices
- Use upload sessions for files > 4 MB
- Implement delta queries for sync scenarios
- Handle conflicts appropriately (rename, replace, fail)
- Cache thumbnails instead of regenerating
- Use batch requests for multiple operations
- Respect rate limits - implement retry logic
- Validate file types before upload
- Use @microsoft.graph.downloadUrl for downloads
- Monitor async operations (copy, large uploads)
- Handle quota limits - check before upload
---
Rate Limits
- Typical limit: Variable based on file size and operation
- Large uploads: Use upload sessions
- Monitor
Retry-Afterheader on 429 responses - Batch operations have separate limits
---
Error Handling
Common errors:
itemNotFound- Item doesn't existresourceModified- Item changed (use etag)unauthenticated- Authentication requiredaccessDenied- Insufficient permissionsquotaLimitReached- Storage quota exceedednameAlreadyExists- Name conflict