How to upload a file from my local computer to a Sharepoint folder

Nelson Soares 0 Reputation points
2025-01-22T20:48:33.8166667+00:00

Hi,



I´m using something like this:





string userName = “nelson.soares@********.pt”;

string password = “*********”;

var securePassword = new SecureString();





foreach (char c in password)

{

 securePassword.AppendChar(c);

}



using (var clientContext = new ClientContext(“https://.sharepoint.com/sites/***/”))

{

 clientContext.Credentials = new NetworkCredential(userName, securePassword);

 Web web = clientContext.Web;

 clientContext.Load(web, a => a.ServerRelativeUrl);

 //clientContext.ExecuteQuery();

 Microsoft.SharePoint.Client.List documentsList = clientContext.Web.Lists.GetByTitle(“01.Diários”);



 var fileCreationInformation = new FileCreationInformation();

 //Assign to content byte[] i.e. documentStream



 fileCreationInformation.Content = System.IO.File.ReadAllBytes(@”C:\test\1. Verificar ficheiros ATM, VivaGO.xlsx”);

 //Allow owerwrite of document



 fileCreationInformation.Overwrite = true;

 //Upload URL



 fileCreationInformation.Url = “https://****.sharepoint.com/sites/*****/Documentos/General/Reports/02.DFP_e_DMC/”;



 Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);



 //Update the metadata for a field having name “DocType”

 uploadFile.ListItemAllFields[“Title”] = “UploadedviaCSOM”;



 uploadFile.ListItemAllFields.Update();

 clientContext.ExecuteQuery(enter code here);



}



string userName = “nelson.soares@********.pt”;

string password = “*********”;

var securePassword = new SecureString();





foreach (char c in password)

{

 securePassword.AppendChar(c);

}



using (var clientContext = new ClientContext(“https://.sharepoint.com/sites/***/”))

{

 clientContext.Credentials = new NetworkCredential(userName, securePassword);

 Web web = clientContext.Web;

 clientContext.Load(web, a => a.ServerRelativeUrl);

 //clientContext.ExecuteQuery();

 Microsoft.SharePoint.Client.List documentsList = clientContext.Web.Lists.GetByTitle(“01.Diários”);



 var fileCreationInformation = new FileCreationInformation();

 //Assign to content byte[] i.e. documentStream



 fileCreationInformation.Content = System.IO.File.ReadAllBytes(@”C:\test\1. Verificar ficheiros ATM, VivaGO.xlsx”);

 //Allow owerwrite of document



 fileCreationInformation.Overwrite = true;

 //Upload URL



 fileCreationInformation.Url = “https://****.sharepoint.com/sites/*****/Documentos/General/Reports/02.DFP_e_DMC/”;



 Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);



 //Update the metadata for a field having name “DocType”

 uploadFile.ListItemAllFields[“Title”] = “UploadedviaCSOM”;



 uploadFile.ListItemAllFields.Update();

 clientContext.ExecuteQuery(enter code here);



}







But it returns 403 Forbidden error.

I´m conected throw VPN and i have MFA account, otherwise I can´t conect to sharepoint, and all the user/pass are corret.

Can you please help to understand the reason thats not working please?

#Read more: https://www.sharepointdiary.com/2012/04/how-to-programmatically-upload-file-to-sharepoint-library.html#ixzz8y65GT87f

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,343 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 30,791 Reputation points Microsoft External Staff
    2025-01-23T02:55:21.2433333+00:00

    Hi @Nelson Soares ,

    Welcome to Q&A forum! Happy to assist you on this issue.

    According to the information you provided, it seems that you are using SharePoint Online, but the reference link you provided is for SharePoint Server. Please confirm that for SharePoint Online, you can directly drag and drop files or click Upload to upload them. If you want to use code, you are recommended to use PowerShell.

    Reference:

    https://www.sharepointdiary.com/2016/06/upload-files-to-sharepoint-online-using-powershell.html

    Non-official, just for business.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.