Sharepoint - getFileByServerRelativeURL fails when the filename contains a quote
Try putting extra apostrophe. That is, use Web/getfilebyserverrelativeurl("/Shared Documents/Roy''s quoted doc.txt")
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.