I want to upload a file to a sub folder of a document Library in Sharepoint Online.
I have an error when posting the file: "URL is not web relative". I don't understand because I can read the content of the sub folder with a GET Request.
My file full path exists, I can read it and transform it to a ByteArrayContent.
My code:
// Upload
using (HttpClient client = new HttpClient())
{
// Access Token with Bearer
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", $"Bearer { accessToken }");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json;odata=verbose");
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json;odata=verbose");
try
{
// Ok, I can get the result from this url
string uriGet = $"{ _baseUri }/_api/web/getFolderByServerRelativeUrl('Shared Documents/MyFolder')";
var resultGet = await client.GetAsync(uriGet);
var stringResultGet = await resultGet.Content.ReadAsStringAsync();
// But, posting a file isn't working on the same folder
string uri = $"{ _baseUri }/_api/web/getFolderByServerRelativeUrl('Shared Documents/MyFolder')/files/add(url='{ fileFullPath }', overwrite=true)";
var bytes = System.IO.File.ReadAllBytes(fileFullPath);
ByteArrayContent byteArrayContent = new ByteArrayContent(bytes);
var result = await client.PostAsync(uri, byteArrayContent);
var stringResult = await result.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
throw;
}
}
Thanks for your help !