The remote server returned an error: (400) Bad Request.

at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.File.OpenBinaryDirect(ClientContext context, tring serverRelativeUrl) at SPConsoleClientModelDemo.Program.GetFoldersAndFiles1(Folder mainFolder, Cemphasized text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.IO;
using Microsoft.SharePoint;
using System.Web.Script.Serialization;
using System.Configuration;
using System.Security;
using System.Net;
using System.Xml;

namespace SPConsoleClientModelDemo
    class Program
       static void Main(string[] args)
            string SharepointURL = ConfigurationManager.AppSettings["SharepointURL"];
            string username = ConfigurationManager.AppSettings["UserName"].ToString();
            string Password = ConfigurationManager.AppSettings["Password"];
            SecureString passWord = new SecureString();
            foreach (char c in Password.ToCharArray())
            ClientContext clientContext = new ClientContext(SharepointURL);
            clientContext.Credentials = new NetworkCredential(username, passWord);
            Web web = clientContext.Web;
            clientContext.Load(web, website => website.Webs, website => website.Title);
            ListCollection lists = web.Lists;
            List list = web.Lists.GetByTitle("Mydocumentlibrary");
            Console.WriteLine("" + list.RootFolder.Name.ToString());
          //  GetFoldersAndFiles11(list.RootFolder, clientContext);


        catch (Exception ex)
            Console.WriteLine(ex.Message + "\n" + ex.StackTrace + "\n\n\n" + ex.Source);


closed as unclear what you're asking by moe, Gautam Sheth, Gaurravs, Mohamed El-Qassas MVP Jan 22 '18 at 10:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I am trying to download all the folders and files from share point site to my network drive. but when I call GetFoldersAndFiles1 method i am getting the above error at two lines in the above code first line is clientContext.ExecuteQuery(); and second one is var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef); and the error message is The remote server returned an error: (400) Bad Request. – Giri Podili Jan 23 '18 at 2:22
  • that is my complete code (i am trying to get all the folders and files from my document library – Giri Podili Jan 23 '18 at 2:40
  • Use below code to get the files from the folder: cxt.Load(list.RootFolder); cxt.Load(list.RootFolder.Folders); cxt.Load(list.RootFolder.Files); cxt.ExecuteQuery(); FolderCollection fcol = list.RootFolder.Folders; List<string> lstFile = new List<string>(); foreach(Folder f in fcol) { if (f.Name == "filename") { cxt.Load(f.Files); cxt.ExecuteQuery(); FileCollection fileCol = f.Files; foreach (File file in fileCol) {} – Deepmala Jan 23 '18 at 3:46
  • Thanks for your quick reply , i am getting this error before i go to files in the root folder in the above example clientContext.ExecuteQuery(); appeared multiple places but getting the error in anyone of the line of (executequery) and that the error is not regular (i.e intermittent) – Giri Podili Jan 23 '18 at 11:32