Uploading large files to SharePoint can be tricky, but Microsoft Graph SDK’s upload sessions make it reliable by splitting files into manageable chunks. Here’s how to do it in C#—no jargon, just clear steps!
An upload session lets you:
Upload large files (e.g., >4MB) in smaller chunks.
Resume uploads if the connection drops.
Avoid timeouts common with single-request uploads.
Install NuGet Packages:
Install-Package Microsoft.Graph
Install-Package Microsoft.Graph.Core
Azure App Registration:
Register your app in Azure AD.
Grant Sites.ReadWrite.All (Application permissions).
Use the ClientSecretCredential
to authenticate your app:
using Microsoft.Graph;
using Azure.Identity;
var tenantId = "YOUR_TENANT_ID";
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(credential);
Specify the SharePoint file path (replace placeholders):
var siteId = "your-site-id"; // SharePoint site ID
var driveId = "your-drive-id"; // Document library ID
var fileName = "largefile.zip"; // File name
var folderPath = "Shared%20Documents"; // URL-encoded folder path
// Request upload session
var uploadSession = await graphClient.Sites[siteId]
.Drives[driveId]
.Root
.ItemWithPath($"{folderPath}/{fileName}")
.CreateUploadSession()
.Request()
.PostAsync();