I'd be able to fetch file using getFileByServerRelativeUrl even if file name contains hash (#).
https://tenant.sharepoint.com/sites/mysc/_api/web/getFileByServerRelativeUrl('%2Fsites%mysc%mydoclib%2Ffolder1%2Ffolder2%2Ffolder3%2F6Doc%23%3B%20Project%20Name.XLS')/$value returns SPException _The file /sites/mysc/mydoclib/folder1/folder2/folder3/6Doc#; Project Name.XLS does not exist_.
I have file in /sites/mysc/mydoclib/folder1/folder2/folder3/6Doc#; Project Name.XLS. I need to get hold of the file (in order to copy it to another site collection), so in my SPFx web part I do
let file = sourceWeb.getFileByServerRelativeUrl(f.Path);
file.getBlob().then(blob => {
// deal with the file
});
where f.Path is encodeURIComponent'ed path of the file, so in the end it makes REST call to
https://tenant.sharepoint.com/sites/mysc/_api/web/getFileByServerRelativeUrl('%2Fsites%mysc%mydoclib%2Ffolder1%2Ffolder2%2Ffolder3%2F6Doc%23%3B%20Project%20Name.XLS')/$value
Not doing encodeURIComponent to the path results same error. If I simply rename the file and remove #, getFileByServerRelativeUrl works without issues.
Why do you must use the # in file name, it is a bad naming conventions/UX. https://support.office.com/en-us/article/invalid-file-names-and-file-types-in-onedrive-onedrive-for-business-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa
I agree, but # is valid in SharePoint Online, so I need to ensure my implementation doesn't break if/when someone has # in file name.
Although the file name have support # in name, but getFileByServerRelativeUrl is old service, it may not have fully adjusted to suit the latest naming rules. You can provide an feature request in UserVocie and wait the Product Group handle it later. Sorry for any inconvenience.
Hi @jpalo If it is possible, could you please give me a full sample code in order to reproduce your issue?
It is already possible using the getFileByServerRelativePath endpoint. Just as you did in this case, you can pass the encodedURIComponented path. It works similar to getFileByServerRelativeUrl endpoint
Modify your code as mentioned below:
let file = sourceWeb.getFileByServerRelativePath(f.Path);
file.getBlob().then(blob => {
// deal with the file
});
We also have added the support in the new @pnp/sp libraries wherein you use the below snippet:
sp.web.getFileByServerRelativePath(f.Path).getBlob().then();
Do note - it only works for SPO and not OnPrem.
References - Web.GetFileByServerRelativePath method and Supporting % and # in files and folders with the ResourcePath API
Can confirm @gautamdsheth solution works great, thank you.
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues
Most helpful comment
It is already possible using the
getFileByServerRelativePathendpoint. Just as you did in this case, you can pass theencodedURIComponented path. It works similar togetFileByServerRelativeUrlendpointModify your code as mentioned below:
We also have added the support in the new @pnp/sp libraries wherein you use the below snippet:
sp.web.getFileByServerRelativePath(f.Path).getBlob().then();Do note - it only works for SPO and not OnPrem.
References - Web.GetFileByServerRelativePath method and Supporting % and # in files and folders with the ResourcePath API