Skip to content

🐞 Bug report: web.GetFolderByIdAsync returned although the entity is a file #1889

Open

Description

@BitRevenant

⭐ Priority

(Medium)😢 I'm annoyed but I'll live

📝 Describe the bug

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.

📷 Screenshots

No response

❓ Environment details

  • SDK version: 1.18.0
  • OS: Windows 11 Enterprise
  • SDK used in: ASP.NET Web API
  • Framework: .NET 10
  • Browser(s): -
  • Tooling: JetBrains Rider 2026.2.2
  • Additional details: -

🤔 Additional context

No response

Activity

  1. added theissue type on Sep 30, 2026
  2. added
    bug
    on Sep 30, 2026
  3. Adam-it commented on Oct 20, 2026

    @Adam-it
    Member

    @BitRevenant thanks for bringing this to our attention 👍. Seems like an overlook from our side.
    Do you want to pick it up yourself?

  4. svermaak commented on Oct 28, 2026

    @svermaak
    Contributor

    @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:

    1. what getFolderById actually returns for a file's UniqueId (a folder-shaped payload for the file itself, its parent folder, or something emptier), and
    2. 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?

  5. added and removed
    help wanted
    on Oct 28, 2026
  6. Adam-it commented on Oct 28, 2026

    @Adam-it
    Member

    @svermaak all yours

  7. svermaak commented on Oct 29, 2026

    @svermaak
    Contributor

    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):

    ---- GetFolderByIdAsync(folder id)    control ----
      RETURNED IFolder   Name='RealFolder' Url='/sites/.../Repro1889/RealFolder'
    
    ---- GetFolderByIdAsync(FILE id) ----
      THREW SharePointRestServiceException
        HttpResponseCode : 404
        ServerErrorCode  : -2147024894
        Code             : System.IO.DirectoryNotFoundException
        Message          : Folder not found.
    
    ---- GetFolderByServerRelativeUrlAsync(FILE path) ----
      THREW SharePointRestServiceException
        HttpResponseCode : 404
        ServerErrorCode  : -2147024894
        Code             : System.IO.DirectoryNotFoundException
        Message          : Folder not found.
    

    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 ListItem GUID 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?

  8. Adam-it commented on Oct 29, 2026

    @Adam-it
    Member

    @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 👍👍

  9. added
    question
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Candidate for fixingbugneeds researchquestionwork in progress

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions