I am trying to upload documents to a specified sharepoint location but I am getting the error System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'
I believe this has something to do with the validation but not 100% on this. Below is my code and the error is returned at the ctx.ExecuteQuery();.
public static void sharepointUpload()
{
String filePath = @"C:\temp\test.pdf";
String siteUrl = "https://<myCompany>.sharepoint.com/";
String documentLibraryName = "PayPoint";
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
string userName = "firstname.surname@companyName.co.uk";
string password = "password";
SecureString securePassword = new SecureString();
foreach (char ch in password.ToCharArray())
securePassword.AppendChar(ch);
SharePointOnlineCredentials credentials =
new SharePointOnlineCredentials(userName, securePassword);
using (ClientContext ctx = new ClientContext(siteUrl))
{
FileCreationInformation fcInfo = new FileCreationInformation();
fcInfo.Url = fileName;
fcInfo.Overwrite = true;
fcInfo.Content = File.ReadAllBytes(filePath);
ctx.Credentials = credentials;
Web myWeb = ctx.Web;
List myLibrary = myWeb.Lists.GetByTitle(documentLibraryName);
myLibrary.RootFolder.Files.Add(fcInfo);
ctx.Load(myWeb, w => w.Title);
ctx.ExecuteQuery();
Console.WriteLine(myWeb.Title);
}
Console.WriteLine("Succesful Upload");
}