79515435

Date: 2025-03-17 18:24:42
Score: 0.5
Natty:
Report link

How to Upload Large Files to SharePoint Using Microsoft Graph SDK in C# (Step-by-Step)

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!


What is an Upload Session?

An upload session lets you:

  1. Upload large files (e.g., >4MB) in smaller chunks.

  2. Resume uploads if the connection drops.

  3. Avoid timeouts common with single-request uploads.


Step 1: Set Up Prerequisites

  1. Install NuGet Packages:

    Install-Package Microsoft.Graph  
    Install-Package Microsoft.Graph.Core  
    
  2. Azure App Registration:

    • Register your app in Azure AD.

    • Grant Sites.ReadWrite.All (Application permissions).


Step 2: Authenticate with Microsoft Graph

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);  

Step 3: Create the Upload Session

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();  

Read More...

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): How to
  • Low reputation (1):
Posted by: dooo. xxturtu