Graph API Drive Items creating a folder under the root

MartienJacobs-5699 0 Reputation points
2026-05-05T14:11:52.8333333+00:00

I'm using a lot of functions from msgraph. Now I stuck on creating a folder under the root.
This will give an invalid request. Already searched a lot sites, used copilot to get answer. But with no results.

 addedDriveItem = await graphClient
     .Drives[driveItemId]
     .Items["root"]
     .Children
     .PostAsync(driveItem);
var driveItem = new DriveItem
{
    Name = PrepareDriveItemName(dossierName).Trim(),
    Folder = new Microsoft.Graph.Models.Folder(),
    AdditionalData = new Dictionary<string, object>
    {
        { "@microsoft.graph.conflictBehavior", "fail" },
    },
};

Already tried this:


var requestInfo = graphClient
    .Drives[clientRootDriveId]
    .Items["root"]
    .Children
    .ToPostRequestInformation(driveItem);

addedDriveItem = await graphClient
    .RequestAdapter
    .SendAsync<DriveItem>(
        requestInfo,
        DriveItem.CreateFromDiscriminatorValue);


and

addedDriveItem = await graphClient
    .Drives[clientRootDriveId]
    .Items[driveItemId]
    .Children
    .PostAsync(driveItem);

Also looked at the application rights and I think I don't miss any.

Microsoft Security | Microsoft Graph

1 answer

Sort by: Most helpful
  1. AI answer

    2026-05-05T14:12:05.28+00:00

    To create a folder directly under the root of a drive with the Microsoft Graph SDK, the request must target the drive’s root item correctly. The invalid request comes from using Items["root"], which expects an item ID, not the literal string "root".

    Use the Root navigation or the /root/children pattern instead of Items["root"].

    For example, the REST call to create a folder in the signed-in user’s OneDrive root is:

    POST https://graph.microsoft.com/v1.0/me/drive/root/children
    Content-Type: application/json
    
    {
      "name": "New Folder",
      "folder": { },
      "@microsoft.graph.conflictBehavior": "fail"
    }
    

    The equivalent SDK pattern is (conceptually):

    var driveItem = new DriveItem
    {
        Name = PrepareDriveItemName(dossierName).Trim(),
        Folder = new Microsoft.Graph.Models.Folder(),
        AdditionalData = new Dictionary<string, object>
        {
            { "@microsoft.graph.conflictBehavior", "fail" },
        },
    };
    
    // For the current user’s default drive root
    var addedDriveItem = await graphClient
        .Me
        .Drive
        .Root
        .Children
        .PostAsync(driveItem);
    
    // Or, for a specific drive by ID
    var addedDriveItem2 = await graphClient
        .Drives[clientRootDriveId]
        .Root
        .Children
        .PostAsync(driveItem);
    

    Key points:

    1. Do not use Items["root"] when the SDK expects an item ID.
    2. To create under root, use:
      • Me.Drive.Root.Children.PostAsync(...), or
      • Drives[drive-id].Root.Children.PostAsync(...).
    3. Ensure the app has at least Files.ReadWrite (delegated) or Files.ReadWrite.All (application) permissions as documented for driveItem-post-children.

    If creating under a subfolder instead of root, use the parent folder’s item-id:

    var addedDriveItem = await graphClient
        .Drives[clientRootDriveId]
        .Items[parentItemId]
        .Children
        .PostAsync(driveItem);
    

    This matches the documented POST /drives/{drive-id}/items/{parent-item-id}/children pattern.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.