I need to upload a CSV or Excel file to a SharePoint folder using a C# application. Could you please assist with this?
How can this be accomplished in C#?
Loading
I need to upload a CSV or Excel file to a SharePoint folder using a C# application. Could you please assist with this?
How can this be accomplished in C#?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 20, 2024, 4:11 AM EST
Hi @Jignesh Kumar,
To upload an Excel or CSV file to a SharePoint folder using C#, you can leverage the SharePoint Client Object Model.
using System; using System.IO; using Microsoft.SharePoint.Client; class Program { static void Main(string[] args) { string siteUrl = "https://yoursharepointsite.com"; string folderUrl = "/sites/yoursite/Shared Documents/YourFolder"; string filePath = @"C:\path\to\yourfile.xlsx"; using (ClientContext context = new ClientContext(siteUrl)) { context.Credentials = new SharePointOnlineCredentials("[email protected]", GetSecureString("yourpassword")); using (FileStream fs = new FileStream(filePath, FileMode.Open)) { FileCreationInformation newFile = new FileCreationInformation { ContentStream = fs, Url = Path.GetFileName(filePath), Overwrite = true }; Microsoft.SharePoint.Client.File uploadFile = context.Web.GetFolderByServerRelativeUrl(folderUrl).Files.Add(newFile); context.Load(uploadFile); context.ExecuteQuery(); } } } private static SecureString GetSecureString(string password) { SecureString secureString = new SecureString(); foreach (char c in password) { secureString.AppendChar(c); } return secureString; } }NOTE :- Replace the placeholders with your actual SharePoint site URL, folder path, and file details. This code establishes a connection to SharePoint, reads the specified file, and uploads it to the designated folder. Ensure you have the necessary permissions and references to the SharePoint Client assemblies in your project.
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 20, 2024, 4:15 AM EST
In which version it will work ?
context.Credentials = new SharePointOnlineCredentials("[email protected]", GetSecureString("yourpassword"));I am not getting this one SharePointOnlineCredentials()
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 20, 2024, 5:51 AM EST
Hi
The
SharePointOnlineCredentialsclass is part of the SharePoint Client Components SDK, which is compatible with .NET Framework versions 4.5 and later. This class is specifically designed for authenticating against SharePoint Online using the SharePoint Client Object Model.To utilize
SharePointOnlineCredentials, ensure that you have the appropriate NuGet package installed, typicallyMicrosoft.SharePointOnline.CSOM.Once the package is installed, you should be able to use
SharePointOnlineCredentialswithout any issues. If you are still encountering problems, verify that your project is targeting a compatible version of the .NET Framework and that all necessary references are correctly set up.context.Credentials = new SharePointOnlineCredentials("[email protected]", GetSecureString("yourpassword"));This line effectively sets the credentials for the
ClientContext, allowing you to interact with SharePoint Online securely.Vishal YelveRank #178 · 13.6K reputationPosted Nov 20, 2024, 10:42 AM EST
Hi Jignesh,
https://learn.microsoft.com/en-us/answers/questions/1517434/how-to-upload-a-file-to-a-sharepoint-driveitem-fol
https://medium.com/@mitchelldalehein25/uploading-files-to-sharepoint-using-microsoft-graph-api-c-0edc689fb388