You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🐞 Bug report: web.GetFolderByIdAsync returned although the entity is a file #1889
As writter in the title, if I use the method web.GetFolderByIdAsync (namespace PnP.Core.Model.SharePoint.IWeb) by passing a file Id, it returns as IFolder.
This does not happen if I call web.GetFolderByServerRelativeUrlAsync.
I have not tested the synchronous versions but they might have the same problem.
👣 Steps To Reproduce
See description.
📜 Expected behavior
The method should throw SharePointRestServiceException with SharePointRestError.HttpStatusCode as HttpStatusCode.NotFound as it happens for the method web.GetFolderByServerRelativeUrlAsync.
@Adam-it@BitRevenant happy to take this one if @BitRevenant isn't already on it - please say so if you are and I'll leave it alone.
I had a look at where the asymmetry comes from before offering. The two methods talk to different endpoints:
GetFolderByServerRelativeUrlAsync calls _api/Web/getFolderByServerRelativePath(decodedUrl=@u), which returns 404 / -2147024894 when the path resolves to a file, so the catch in that method rewrites it to System.IO.DirectoryNotFoundException and it surfaces as the reporter expects.
GetFolderByIdAsync calls _api/Web/getFolderById(guid'{id}'), which looks like it resolves the unique id without caring about FSObjType, so nothing throws and the catch that's already sitting in that method never fires.
Two things I'd want to confirm on a tenant before proposing a fix, since the right fix depends on them:
what getFolderById actually returns for a file's UniqueId (a folder-shaped payload for the file itself, its parent folder, or something emptier), and
whether the sync and the four GetFolderByIdBatch* overloads behave the same - those batch overloads have no error handling at all today, so they may need covering too.
If it turns out the payload is distinguishable, the fix stays local to this region of Web.cs. If it isn't, it needs an extra piece of data in the request and that's a bigger discussion, which I'd bring back here before writing code rather than guessing.
Could you assign it to me if you're happy with that?
Thanks @Adam-it. I've reproduced against current dev on a test tenant and I can't make this fail - and my guess in the comment above about why it fails was wrong, so let me correct that first.
I was wrong that getFolderById ignores FSObjType. It doesn't. SharePoint rejects a file id on that endpoint just like it rejects a file path on the path endpoint:
GET /_api/Web/getFolderById(guid'<file UniqueId>')
-> 404 {"code":"-2147024894, System.IO.FileNotFoundException","value":"File Not Found."}
GET /_api/Web/getFolderByServerRelativePath(decodedUrl='<file path>')
-> 404 {"code":"-2147024894, System.IO.FileNotFoundException","value":"File Not Found."}
I checked that against every kind of file id I could think of, in case it's library or content-type specific - a file in a custom document library, a file in a nested folder, a file in the default Documents library, an .aspx page in SitePages, and a list item attachment. All five 404. A real folder id and the library root folder id both return normally, so the endpoint is behaving.
And GetFolderByIdAsync does throw, with exactly the error the issue asks for. Output from a console app against dev (6a6b8d892):
The two are identical, and GetFolderById (sync) matches too.
That also holds for the reporter's version: the normalization in #1725 / 8e347b2 is contained in tags 1.16.0, 1.17.0 and 1.18.0, and it added the same try/catch to both methods in one commit. Before that commit neither method had any error handling, so there was no version where the two differed in the way described.
@BitRevenant could you share a bit more, since you're clearly hitting something I'm not:
how you get the id you pass in - IFile.UniqueId, a Graph driveItem id, a ListItemGUID field, something else? A Graph driveItem id in particular is a different identifier and won't resolve here.
what kind of library the file lives in, and whether it's anything special (document set, OneNote, checked out, in a sub-site, OneDrive rather than a team site).
whether the folder you get back has real values for Name / ServerRelativeUrl, and if so what they are - if it's returning your file's path that's a different bug from it returning something empty.
the exact call, including whether it was a batch overload.
One real gap I did find
The batch overloads skip the #1725 normalization, because the try/catch only exists in the two non-batch methods:
call
Code
Message
GetFolderByIdAsync
System.IO.DirectoryNotFoundException
Folder not found.
GetFolderByServerRelativeUrlAsync
System.IO.DirectoryNotFoundException
Folder not found.
GetFolderByIdBatchAsync
System.IO.FileNotFoundException
File Not Found.
GetFolderByServerRelativeUrlBatchAsync
System.IO.FileNotFoundException
File Not Found.
So the inconsistency is batch vs non-batch rather than by-id vs by-path, and it affects both methods equally. Caller code that checks for DirectoryNotFoundException works for a direct call and silently fails for the same call in a batch.
Happy to fix that - it's small, though it's not quite what this issue is about and the normalization would have to happen where the batch response is processed rather than around the Add. Do you want it here, or as its own issue so #1889 stays open for whatever @BitRevenant is actually hitting?
@svermaak thanks for the research 👍. I will look into it myself but in the meantime @BitRevenant may I kindly ask you to provide a bit more details @svermaak asked for in the comment above. Indeed, those will prove helpful 👍👍
Activity
Adam-it commentedon Oct 20, 2026
@BitRevenant thanks for bringing this to our attention 👍. Seems like an overlook from our side.
Do you want to pick it up yourself?
svermaak commentedon Oct 28, 2026
@Adam-it @BitRevenant happy to take this one if @BitRevenant isn't already on it - please say so if you are and I'll leave it alone.
I had a look at where the asymmetry comes from before offering. The two methods talk to different endpoints:
GetFolderByServerRelativeUrlAsynccalls_api/Web/getFolderByServerRelativePath(decodedUrl=@u), which returns 404 /-2147024894when the path resolves to a file, so thecatchin that method rewrites it toSystem.IO.DirectoryNotFoundExceptionand it surfaces as the reporter expects.GetFolderByIdAsynccalls_api/Web/getFolderById(guid'{id}'), which looks like it resolves the unique id without caring aboutFSObjType, so nothing throws and thecatchthat's already sitting in that method never fires.Two things I'd want to confirm on a tenant before proposing a fix, since the right fix depends on them:
getFolderByIdactually returns for a file'sUniqueId(a folder-shaped payload for the file itself, its parent folder, or something emptier), andGetFolderByIdBatch*overloads behave the same - those batch overloads have no error handling at all today, so they may need covering too.If it turns out the payload is distinguishable, the fix stays local to this region of
Web.cs. If it isn't, it needs an extra piece of data in the request and that's a bigger discussion, which I'd bring back here before writing code rather than guessing.Could you assign it to me if you're happy with that?
Adam-it commentedon Oct 28, 2026
@svermaak all yours
svermaak commentedon Oct 29, 2026
Thanks @Adam-it. I've reproduced against current
devon a test tenant and I can't make this fail - and my guess in the comment above about why it fails was wrong, so let me correct that first.I was wrong that
getFolderByIdignoresFSObjType. It doesn't. SharePoint rejects a file id on that endpoint just like it rejects a file path on the path endpoint:I checked that against every kind of file id I could think of, in case it's library or content-type specific - a file in a custom document library, a file in a nested folder, a file in the default
Documentslibrary, an.aspxpage inSitePages, and a list item attachment. All five 404. A real folder id and the library root folder id both return normally, so the endpoint is behaving.And
GetFolderByIdAsyncdoes throw, with exactly the error the issue asks for. Output from a console app againstdev(6a6b8d892):The two are identical, and
GetFolderById(sync) matches too.That also holds for the reporter's version: the normalization in #1725 / 8e347b2 is contained in tags 1.16.0, 1.17.0 and 1.18.0, and it added the same
try/catchto both methods in one commit. Before that commit neither method had any error handling, so there was no version where the two differed in the way described.@BitRevenant could you share a bit more, since you're clearly hitting something I'm not:
IFile.UniqueId, a GraphdriveItemid, aListItemGUIDfield, something else? A Graph driveItem id in particular is a different identifier and won't resolve here.Name/ServerRelativeUrl, and if so what they are - if it's returning your file's path that's a different bug from it returning something empty.One real gap I did find
The batch overloads skip the #1725 normalization, because the
try/catchonly exists in the two non-batch methods:CodeMessageGetFolderByIdAsyncSystem.IO.DirectoryNotFoundExceptionGetFolderByServerRelativeUrlAsyncSystem.IO.DirectoryNotFoundExceptionGetFolderByIdBatchAsyncSystem.IO.FileNotFoundExceptionGetFolderByServerRelativeUrlBatchAsyncSystem.IO.FileNotFoundExceptionSo the inconsistency is batch vs non-batch rather than by-id vs by-path, and it affects both methods equally. Caller code that checks for
DirectoryNotFoundExceptionworks for a direct call and silently fails for the same call in a batch.Happy to fix that - it's small, though it's not quite what this issue is about and the normalization would have to happen where the batch response is processed rather than around the
Add. Do you want it here, or as its own issue so #1889 stays open for whatever @BitRevenant is actually hitting?Adam-it commentedon Oct 29, 2026
@svermaak thanks for the research 👍. I will look into it myself but in the meantime @BitRevenant may I kindly ask you to provide a bit more details @svermaak asked for in the comment above. Indeed, those will prove helpful 👍👍