Forum Discussion
Navigating Sharepoint Folders Containing Apostrophes
Hi,
If a sharepoint is accessed via sharepoint.contents(url)
and then navigated like nextFolder = Source{[Name = "Folder"]}[Content]
if any folders contain an apostrophe, e.g. John Smith's Folder, power query returns:
DataSource.Error: SharePoint: Request failed: The remote server returned an error: (400) Bad Request. (The expression "Web/GetFolderByServerRelativePath(decodedurl='/John Smith's Folder')/Folders" is not valid.)
Obviously this is because the ' in Smith's effectively ends the string.
If, instead, an escape character is added, e.g. '' rather than ', this then returns key didn't match any rows in table.
I cannot find a way to satisfy both the name= part and the api url part in one query.
My current fix is removing apostrophes from folder names but would like the ability to use any folder that windows deems is named acceptably
Hey, LeverPowerCLiff ,
This is kinda big oopsie from the SharePoint site; There's no native clicking way to bypass this. But for you, it's an opportunity to learn a different way how to get your data (perhaps even faster) than the standard way.
The error already hints at what SharePoint does in the background, and you can do it just yourself. I kinda wrote a blog about this, so for full context, you can check it here: https://www.vojtechsima.com/post/fix-slow-refreshes-faster-way-to-load-sharepoint-files-in-power-bi
But to resolve your issues immediately, you can do this:
let // config here tenantUrl = "https://daatlers.sharepoint.com", siteUrlPath = "sites/Experiments", targetFolderServerRelativeUrl = "/" & siteUrlPath & "/Shared Documents/Vojtech's Folder", targetFolderServerRelativeUrlQuoteEscaped = Text.Replace(targetFolderServerRelativeUrl, "'", "''"), getListOfFiles = Json.Document( Web.Contents(tenantUrl, [ RelativePath = siteUrlPath & "/_api/web/GetFolderByServerRelativeUrl('" & targetFolderServerRelativeUrlQuoteEscaped & "')/Files", Headers = [Accept="application/json;odata=nometadata"] ] ) ),I entered my own information, so it's clearer for you. This will output a list of records where you have all the files in your SharePoint for the given site and folder. You can also see that I added escaping of the quote character.
Then you get a list of files, and for each you get their 'downloadable' path, and that's kinda it, then you can transform them or do anything you need to do with clicking. The example 'extractor' could look like this:
extractServerRelativeUrlAndBuffer = List.Transform(getListOfFiles[value], each [ServerRelativeUrl]), retrieveContentForFile = (serverRelativeUrl as text) => let binaryContent = Web.Contents(tenantUrl, [ RelativePath = siteUrlPath & "/_api/web/GetFileByServerRelativeUrl('" & Text.Replace(serverRelativeUrl, "'", "''") & "')/$value" ] ), // transformations here processedData = binaryContent in processedData, combineAllFiles = List.Transform(extractServerRelativeUrlAndBuffer, each retrieveContentForFile(_)) in combineAllFilesWith this approach, you first enter the string to the target folder, you want to access, so you don't go folder by folder, but you directly locate the folder and then you get list of files with their metadata, here you can expand it to table and modify the list with clicking, or if you want everything, just extract the download link and then get the binaries, and then you can treat it as any other file or set of files.
Lemme know what you think.
8 Replies
- vojtechsimaSuper User
Hey, LeverPowerCLiff ,
This is kinda big oopsie from the SharePoint site; There's no native clicking way to bypass this. But for you, it's an opportunity to learn a different way how to get your data (perhaps even faster) than the standard way.
The error already hints at what SharePoint does in the background, and you can do it just yourself. I kinda wrote a blog about this, so for full context, you can check it here: https://www.vojtechsima.com/post/fix-slow-refreshes-faster-way-to-load-sharepoint-files-in-power-bi
But to resolve your issues immediately, you can do this:
let // config here tenantUrl = "https://daatlers.sharepoint.com", siteUrlPath = "sites/Experiments", targetFolderServerRelativeUrl = "/" & siteUrlPath & "/Shared Documents/Vojtech's Folder", targetFolderServerRelativeUrlQuoteEscaped = Text.Replace(targetFolderServerRelativeUrl, "'", "''"), getListOfFiles = Json.Document( Web.Contents(tenantUrl, [ RelativePath = siteUrlPath & "/_api/web/GetFolderByServerRelativeUrl('" & targetFolderServerRelativeUrlQuoteEscaped & "')/Files", Headers = [Accept="application/json;odata=nometadata"] ] ) ),I entered my own information, so it's clearer for you. This will output a list of records where you have all the files in your SharePoint for the given site and folder. You can also see that I added escaping of the quote character.
Then you get a list of files, and for each you get their 'downloadable' path, and that's kinda it, then you can transform them or do anything you need to do with clicking. The example 'extractor' could look like this:
extractServerRelativeUrlAndBuffer = List.Transform(getListOfFiles[value], each [ServerRelativeUrl]), retrieveContentForFile = (serverRelativeUrl as text) => let binaryContent = Web.Contents(tenantUrl, [ RelativePath = siteUrlPath & "/_api/web/GetFileByServerRelativeUrl('" & Text.Replace(serverRelativeUrl, "'", "''") & "')/$value" ] ), // transformations here processedData = binaryContent in processedData, combineAllFiles = List.Transform(extractServerRelativeUrlAndBuffer, each retrieveContentForFile(_)) in combineAllFilesWith this approach, you first enter the string to the target folder, you want to access, so you don't go folder by folder, but you directly locate the folder and then you get list of files with their metadata, here you can expand it to table and modify the list with clicking, or if you want everything, just extract the download link and then get the binaries, and then you can treat it as any other file or set of files.
Lemme know what you think.
- LeverPowerCLiffFrequent Visitor
Thank you for your reply.
I played around with this for a while, and really like it for my own use. I also read the blog post you linked, it was good to have the additional information found there. The reason I will not implement it into wider production is because the ease of use of sharepoint.contents makes the power query logic far more accessible to others.
If you need to use sharepoint, with a path with an apostrophe, then this appears to be the best solution.
Thank you, again!
- lbendlinSuper User
Removing apostrophes is absolutely the best way to tackle that. But if you cannot do that then remember that you can navigate the Graph via the object ID rather than the object name.
- v-tejramaCommunity Support
Hi LeverPowerCLiff ,
The issue stems from how Power Query generates the SharePoint API call when a folder name contains an apostrophe, which disrupts the request string and results in a bad request error. Escaping the apostrophe by doubling it does not resolve the problem, as Power Query still searches for the exact folder name and the lookup fails.
Using the folder or object ID instead of the name is a reliable solution. Since IDs are not affected by string formatting, they eliminate complications caused by special characters. This approach is consistent with SharePoint’s design, where accessing items by ID is generally more robust, particularly when special characters are present.
If renaming folders is not feasible in your environment, adopting ID-based navigation is the most dependable method to address this issue. It avoids ambiguity from characters like apostrophes and ensures your queries remain stable.
Thank you.- v-tejramaCommunity Support
Hi LeverPowerCLiff ,
I wanted to follow up and see if you had a chance to review the information shared. If you have any further questions or need additional assistance, feel free to reach out.
Thank you.- v-tejramaCommunity Support
Hi LeverPowerCLiff ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.
Thank you.
- Poojara_D12Super User
This happens because Power Query internally builds a SharePoint REST API call where folder paths are wrapped in single quotes, so an apostrophe in a folder name (like Smith’s) breaks the URL syntax and causes the 400 error. When you try to escape it with double single-quotes (''), it may fix the API string but then no longer matches the actual folder name in the navigation table, which is why you get “key didn’t match any rows.” The correct approach is not to manually navigate using {[Name="..."]} for such cases, but instead rely on filtering the table returned by SharePoint.Contents (or SharePoint.Files) using functions like Text.Replace or Text.Contains, which avoids constructing a fragile server-relative path. In short, the issue isn’t that apostrophes are unsupported—it’s that the navigation syntax you’re using conflicts with how the API encodes paths, so the robust solution is to switch from direct record navigation to row filtering, which handles special characters safely.