11

I'm using the SharePoint online REST API. Things generally work fine, but I'm failing to figure out how to get information about a file (document) that contains a quote in its name. Here's my REST call:

https://mysite.sharepoint.com/_api/Web/getfilebyserverrelativeurl("/Shared Documents/Roy's quoted doc.txt")

The response I'm getting is '400 Bad Request', with the error being "The expression "Web/getfilebyserverrelativeurl("/Shared Documents/Roy's quoted doc.txt")" is not valid."

I've tried all sorts of tricks with encoding the path (%27 encoding for the quote sign, backslashing the quote, etc.), but failed to figure out the right one.

Is there any kind of encoding I'm not aware of?

14

Try putting extra apostrophe. That is, use Web/getfilebyserverrelativeurl("/Shared Documents/Roy''s quoted doc.txt")

0

I'll add on to Nadeem's answer by providing a test that I did.

I got 400 errors when I tried to use this as suggested in this answer:

Web/getfilebyserverrelativeurl("/Shared Documents/Roy''s quoted doc.txt")

I instead successfully used single quotes:

Web/getfilebyserverrelativeurl('/Shared Documents/Roy''s quoted doc.txt')

So then I went further. Here are some files that I uploaded to sharepoint online:

https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got ().txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got '' in the title.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got +.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got $ in the title.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got @.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got !.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got ^.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got ‒–—―⁓.txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got [{}].txt
https://xxx.sharepoint.com/Shared Documents/nasty chars/dang dude you got ' in the title.txt

You can work-around the ' by using a special mapping

  • ' --> ''

But there are a couple situations that simply do not work:

If there is a % or $ in the filename of the list item file name, you cannot use getfilebyserverrelativeurl to get them.

So what do you do then?

If you, like me, were simply trying to download files when using the AppOnly authentication (bearer token) then you need to change to the following URL format:

$siteCollectionURL/_layouts/15/download.aspx?SourceUrl=$encodedSiteRelativeURL

This works 100% of the time for all different URLs, and you do not have to do the special ' --> '' replacement.

Example: https://xxx.sharepoint.com//_layouts/15/download.aspx?SourceUrl=%2FShared%20Documents%2Fnasty%20chars%2Fdang%20dude%20you%20got%20%24%20in%20the%20title.txt

The only other option is to get the file by GUID which I did not try but I assume it will work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.