when I am trying to upload a file using FileCreationInformation, I am getting a http 400 bad request error at the context.executequery() call.
I can upload the file successfully using SaveBinaryDirect with MemoryStream or FileStream, but the uploaded file doesn't have any content/data.
Code using SaveBinaryDirect:
string siteUrl = "https://xxx.sharepoint.com/sites/my-site/";
string username = "[email protected]";
string password = "ss@sf";
string filePath = "filePath.xlsx"
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(username, securePWD);
FileInfo fileInfo = new FileInfo(filePath);
byte[] fileContent = File.ReadAllBytes(filePath);
using (MemoryStream memoryStream = new MemoryStream(fileContent))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/ServerRelativeURL/" + fileInfo.Name, memoryStream, true);
}
}
But when I am trying to use FileCreationInformation, I get the 400 bad request error:
string siteUrl = "https://xxh.sharepoint.com/sites/my-Test-Site/";
string username = "[email protected]";
string password = "sad@fds";
string serverRelativeUrl = "Shared Documents/myFolder";
string filePath = "filePath.xlsx"
using (ClientContext context = new ClientContext(siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(username,securePWD);
var fileCreationInfo = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(filePath),
Overwrite = true,
Url = Path.GetFileName(filePath)
};
var targetFolder = context.Web.GetFolderByServerRelativeUrl(serverRelativeUrl);
var uploadFile = targetFolder.Files.Add(fileCreationInfo);
context.Load(uploadFile);
context.ExecuteQuery();
}
I have tried changing my serverRelativeUrl to "Documents" thinking it is asking for the library name, but I get the same error.
Even if I simply want to run this code, I still get 400 bad request error.
List documentsLibrary = context.Web.Lists.GetByTitle("Documents");
context.Load(documentsLibrary);
context.ExecuteQuery();