Sp-dev-docs: getFileByServerRelativeUrl with # in file name

Created on 25 Sep 2018  路  8Comments  路  Source: SharePoint/sp-dev-docs

Category

  • [ ] Question
  • [ ] Typo
  • [X] Bug
  • [ ] Additional article idea

Expected or Desired Behavior

I'd be able to fetch file using getFileByServerRelativeUrl even if file name contains hash (#).

Observed Behavior

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_.

Steps to Reproduce

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.

csorest help wanted

Most helpful comment

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

All 8 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StfBauer picture StfBauer  路  3Comments

ken-harris picture ken-harris  路  3Comments

patrick-rodgers picture patrick-rodgers  路  3Comments

StfBauer picture StfBauer  路  3Comments

bengtmoss picture bengtmoss  路  3Comments