I've been trying for a while to make this work, but I'm stuck. I've checked all the solutions around here and google, but can't seem to figure out why I keep getting an error.
string targetFolder = "/sites/Test/Shared Documents/TestFolder";
string sourcePath = "C:\\Users\\Name\\Desktop\\TestFolder\\";
string sharepointUrl = "https://mycompany.sharepoint.com";
ClientContext clientContext = new ClientContext(sharepointUrl);
clientContext.Credentials = new SharePointOnlineCredentials(username, securePassword);
Web web = clientContext.Web;
clientContext.Load(web);
Folder folder = web.GetFolderByServerRelativeUrl(targetFolder);
clientContext.Load(folder);
DirectoryInfo directory = new DirectoryInfo(sourcePath);
FileInfo[] files = directory.GetFiles("*.*");
foreach (FileInfo file in files)
{
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(sourcePath + file.Name);
newFile.Url = sourcePath + file.Name;
newFile.Overwrite = true;
folder.Files.Add(newFile);
}
clientContext.ExecuteQuery();
It fails at clientContext.ExecuteQuery() with Microsoft.SharePoint.Client.ServerException: 'File Not Found.'