1

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.'

1
  • Can you check if the newFile.Content is filled? And why you are as newFile.Url doing the FileSystem address? Change it to target folder, right? Jan 27, 2019 at 20:30

2 Answers 2

1

The following code for your reference.

string targetSiteURL = @"https://tenant.sharepoint.com/sites/lz";
string targetFolder = "/sites/Test/Shared Documents/TestFolder";
string sourcePath = "C:\\Users\\Name\\Desktop\\TestFolder\\";

var login = "[email protected]";
var password = "xxx";

var securePassword = new SecureString();

foreach (char c in password)
{
    securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
Web web = ctx.Web;
ctx.Load(web);

Folder folder = web.GetFolderByServerRelativeUrl(targetFolder);
ctx.Load(folder);

DirectoryInfo directory = new DirectoryInfo(sourcePath);
FileInfo[] files = directory.GetFiles("*.*");
foreach (FileInfo file in files)
{
    var filePath = sourcePath + file.Name;
    FileCreationInformation newFile = new FileCreationInformation();               
    newFile.Content = System.IO.File.ReadAllBytes(sourcePath + file.Name);
    newFile.Url = file.Name;
    newFile.Overwrite = true;
    Microsoft.SharePoint.Client.File newspFile=folder.Files.Add(newFile);
}
ctx.ExecuteQuery();
1
  • Thanks for this! I had just a couple of lines different from mine. What actually made the difference and made it work was to add "/sites/Test" to my sharepointUrl which I noted that you had in yours. Thanks a lot!
    – WebDev
    Jan 28, 2019 at 16:03
1

Can you check if the newFile.Content is filled?

And why you are as newFile.Url doing the FileSystem address? Change it to target folder, right?

Or try use:

using (var clientContext = new ClientContext(url))
{
     using (var fs = new FileStream(fileName, FileMode.Open))
     {
         var fi = new FileInfo(fileName);
         var list = clientContext.Web.Lists.GetByTitle(listTitle);
         clientContext.Load(list.RootFolder);
         clientContext.ExecuteQuery();
         var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);

         Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
     }
}
3
  • Yeah it gets filled: newFile.Content {byte[6]} byte[] [0] 116 byte [1] 101 byte [2] 115 byte [3] 116 byte [4] 32 byte [5] 49 byte ... I guess the system address got in there when I was trying something, I have it as "targetPath + file.Name" and it does not work... I tried to use that code you suggested, but it did not work :(
    – WebDev
    Jan 28, 2019 at 1:01
  • you need to edit my script a little bit, learn :) Jan 28, 2019 at 7:05
  • Sorry I forgot to mention, I did edit the script to adapt to my code, but it did not work. Thanks for the learn tip, I'll make sure to do better in the future :)
    – WebDev
    Jan 28, 2019 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.