1

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");
        }
  • Did you try the code snippet below ? – Jerry_MSFT Jun 26 '19 at 2:50
1

fileCreationInformation.url should be the server relative url of the SharePoint folder and then append with file name, not only file name, please modify the code as below, provide the folder server relative url:

   public static void sharepointUpload()
    {
        String filePath = @"D:\temp\test.jpg";
        String siteUrl = "https://tenant.sharepoint.com/sites/sitename/";
        String documentLibraryName = "Documents";
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);

        string userName = "user@tenant.onmicrosoft.com";
        string 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))
        {

            Web myWeb = ctx.Web;
            List myLibrary = myWeb.Lists.GetByTitle(documentLibraryName);
            FileCreationInformation fcInfo = new FileCreationInformation();
            fcInfo.Url = "/sites/dev/Shared Documents/"+fileName;
            fcInfo.Overwrite = true;
            fcInfo.Content = System.IO.File.ReadAllBytes(filePath);

            ctx.Credentials = credentials;
            myLibrary.RootFolder.Files.Add(fcInfo);
            ctx.Load(myWeb, w => w.Title);
            ctx.ExecuteQuery();
            Console.WriteLine(myWeb.Title);

        }
        Console.WriteLine("Succesful Upload");
    }

For example the document library folder url is

https://tenant.sharepoint.com/sites/dev/Shared Documents

Then the relative url is /sites/dev/Shared Documents/

| improve this answer | |
0

According to the docs, FileCreationInformation.Url needs to be in relative or absolute form. Which means server-relative or server-absolute.

So, Url should either be https://<myCompany>.sharepoint.com/PayPoint/test.pdf or /PayPoint/test.pdf it would probably be best if you loaded myList.RootFolder first, then use my myList.RootFolder.ServerRelativeUrl to create the new URL for the file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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