Answered by:
GetFileByServerRelativePath does not work and always returns file not found

Question
-
I was previously using the GetFileByServerRelativeUrl and it was working fine, but the characters # and % are not supported while they are supposed to be supported with GetFileByServerRelativePath, so I changed the code as per below but now it just doesn't work with any files???
public bool DownloadFile(string filepath, out string Base64EncodedFile, out string errormessage) { Base64EncodedFile = string.Empty; errormessage = string.Empty; try { Uri filename = new Uri(filepath); string serverrelative = filename.AbsolutePath; //This old method does not support # or % but works fine //Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(serverrelative); // >> Replaced with this ResourcePath filePathDecoded = ResourcePath.FromDecodedUrl(serverrelative); Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativePath(filePathDecoded); // << Replaced with this context.Load(file); ClientResult<System.IO.Stream> streamResult = file.OpenBinaryStream(); context.ExecuteQuery(); Base64EncodedFile = ConvertToBase64(streamResult.Value); return true; } catch (Exception ex) { errormessage = ex.Message; return false; } } SharepointClient.SharepointClient newupload = new SharepointClient.SharepointClient("https://xxxxxxx.sharepoint.com/sites/xxxxxxxxx/", usernametext.Text, textpassword.Text); newupload.DownloadFile(Url.Text, out EncodedAbs, out errormessage);
If I use the old GetFileByServerRelativeUrl it works just fine... I tried everything but I cannot seem to get to work the GetFileByServerRelativePath ... I can't understand what I'm doing wrong???
Please help!!!
Answers
-
Hi,
Could you try below code.
string targetSiteURL = @"https://tenant.sharepoint.com/sites/lee"; var login = "user@tenant.onmicrosoft.com"; var password = "password"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword); using (ClientContext ctx = new ClientContext(targetSiteURL)) { ctx.Credentials = onlineCredentials; string fileName = "FileWith#%.docx"; var _File = ctx.Web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl($"/sites/lee/MyDoc/{fileName}")); ctx.Load(_File); ctx.ExecuteQuery(); Console.Write(_File.ServerRelativeUrl); Console.WriteLine(); }
Best Regards,
Lee
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.- Marked as answer by Taddeo Zanga Friday, June 14, 2019 11:05 PM
All replies
-
Hi Toddeo,
It seems like decoding changed the URL and it doesn't match the path. Can you share the URL for which you are facing the issue.
Best Regards,
Brij K
http://bloggerbrij.blogspot.co.uk/ -
-
Hi,
Could you try below code.
string targetSiteURL = @"https://tenant.sharepoint.com/sites/lee"; var login = "user@tenant.onmicrosoft.com"; var password = "password"; var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword); using (ClientContext ctx = new ClientContext(targetSiteURL)) { ctx.Credentials = onlineCredentials; string fileName = "FileWith#%.docx"; var _File = ctx.Web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl($"/sites/lee/MyDoc/{fileName}")); ctx.Load(_File); ctx.ExecuteQuery(); Console.Write(_File.ServerRelativeUrl); Console.WriteLine(); }
Best Regards,
Lee
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.- Marked as answer by Taddeo Zanga Friday, June 14, 2019 11:05 PM
-
That’s great! Thank you your example opened my eyes and I discovered that the only problem with my code was the URL that I was passing contained encoded HTML characters!!! I was passing the spaces as %20 and this caused the problem!!! Once I remove %20 and replaced with the space all was good! It makes sense as the FromDecodedUrl was actually encoding again the %20 resulting in an invalid URL!