0

I am using following code to upload file at the following location but getting File Not Found error at last line "clientcontext.ExecuteQuery();"

using (ClientContext clientcontext = new ClientContext(txtSiteURL.Text))
{
      string fileToUpload = @"c:\test.txt";
      String fileName = System.IO.Path.GetFileName(fileToUpload);

      Folder folder = clientcontext.Web.Lists.GetByTitle("Docs").RootFolder;

      string documentUrl = txtSiteURL.Text + "/Docs/" + fileName;

      FileCreationInformation fci = new FileCreationInformation();
      fci.Overwrite = true;
      fci.Url = documentUrl;
      fci.Content = System.IO.File.ReadAllBytes(fileToUpload );

      Microsoft.SharePoint.Client.File uploadedFile = folder.Files.Add(fci);
      clientcontext.ExecuteQuery();
}

What am I doing wrong?

4
  • 1
    Helpful? stackoverflow.com/a/9849835/318776 Commented Aug 3, 2015 at 17:20
  • I used the above code and only changed one line "string target = "myserver/sites/website/Documents" + fileName;" but getting same error. Commented Aug 3, 2015 at 18:35
  • Taking a clue from your linkI found what the problem was. My path defined in "target" was wrong. I was using name "Documents" in URL when in fact its internal name is "Shared Documents" Commented Aug 3, 2015 at 18:38
  • in my case it was the / character in filename
    – Liero
    Commented Nov 5, 2022 at 14:14

1 Answer 1

0

Try replacing line 8 from your code with this:

string documentUrl = txtSiteURL.Text + "Docs/" + fileName;

If the txtSiteURL.Text parameter already comes with a / at the end. the "/Docs/" string will cause a double forward slash in the url, and this bad url will cause a "File Not Found" error. (At least this was what caused that same error for me)

Also, try to debug that line and double check the fileName string does not generate problems like this, and check for other issues or typos.

Fixing the url resolved the "File Not Found" error for me.

Your Answer

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

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