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