In this blog post, we’ll walk you through a step-by-step explanation of how to upload files to SharePoint using Microsoft Graph and the PnP Framework.
Pakages
Microsoft.AspNetCore.Mvc, Microsoft.SharePoint.Client, Microsoft.Graph.Models, PnP.Framework.
Prerequisites
There are a few prerequisites to ensure this process runs smoothly:
- Microsoft SharePoint Site: You should have access to a SharePoint site where you want to upload the files.
- Azure AD Application: Set up an Azure Active Directory application and obtain the Client ID and Client Secret. You can do this through the Azure portal.
- Microsoft Graph SDK: Ensure that you have the Microsoft Graph SDK and PnP Framework installed in your project.
Define your ClientId
, ClientSecret
in appsettings.json
public SharePointController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpPost("upload")]
public IActionResult UploadFile()
{
try
{
string siteUrl = "Your/ Site/ Url";
string clientId = _configuration["Azure:ClientId"];
string clientSecret = _configuration["Azure:ClientSecret"];
var authManager = new AuthenticationManager();
var context = authManager.GetACSAppOnlyContext(siteUrl, clientId, clientSecret);
var libraryName = "Documents";//your Library name
var filePath = "Your local file Path";
var folderName = "DestinationForlderName";//Sharpoint Folder Name
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
FileCreationInformation fileInfo = new FileCreationInformation
{
ContentStream = fs,
Url = Path.GetFileName(filePath)
};
var library = context.Web.Lists.GetByTitle(libraryName);
var folder = library.RootFolder.Folders.GetByUrl(folderName);
var file = folder.Files.Add(fileInfo);
context.ExecuteQuery();
return Ok("File uploaded successfully");
}
}
catch (Exception ex)
{
return BadRequest($"Error: {ex.Message}");
}
}
Now, let’s break down the code step by step to understand how it uploads a file to SharePoint.
The UploadFile
method is an HTTP POST endpoint that handles file uploads to SharePoint. It retrieves the SharePoint site URL, Client ID, and Client Secret from the application’s configuration.
Then, we create an AuthenticationManager
instance and use it to obtain an application-only context for SharePoint. We specify the target library name, the local file path, and the folder within SharePoint where the file should be uploaded.
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
FileCreationInformation fileInfo = new FileCreationInformation
{
ContentStream = fs,
Url = Path.GetFileName(filePath)
};
var library = context.Web.Lists.GetByTitle(libraryName);
var folder = library.RootFolder.Folders.GetByUrl(folderName);
var file = folder.Files.Add(fileInfo);
context.ExecuteQuery();
}
This section handles the actual file upload. It opens the local file using a FileStream, creates FileCreationInformation
, and adds the file to the specified folder in SharePoint.
Now, You can see the file in sharepoint destination folder.
In this blog post, we’ve shown how to upload files to SharePoint using Microsoft Graph and the PnP Framework.
No Comment! Be the first one.