admin管理员组

文章数量:1388068

How to create a new folder in root of some One Drive via Microsoft Graph SDK for C#?

Documentation suggests to use HTTP post request to "me/drive/root/children" or "/drives/{drive-id}/root/children". But Root request builder does not have Children collection, so the code can not be compiled:

await client.Drives[someDriveId].Root.Children.PostAsync(newFolderItem);

Is it safe to use ItemWithPath("/") for this case?

await client.Drives[someDriveId].Root.ItemWithPath("/").Children.PostAsync(newFolderItem)

Update: How this question has appeared. I'm updating the code from using MsGraph C# SDK 4.x to MsGraph C# SDK 5.x and found that Root doesn't contain Children property anymore.

P.S.:

How to create a new folder in root of some One Drive via Microsoft Graph SDK for C#?

Documentation suggests to use HTTP post request to "me/drive/root/children" or "/drives/{drive-id}/root/children". But Root request builder does not have Children collection, so the code can not be compiled:

await client.Drives[someDriveId].Root.Children.PostAsync(newFolderItem);

Is it safe to use ItemWithPath("/") for this case?

await client.Drives[someDriveId].Root.ItemWithPath("/").Children.PostAsync(newFolderItem)

Update: How this question has appeared. I'm updating the code from using MsGraph C# SDK 4.x to MsGraph C# SDK 5.x and found that Root doesn't contain Children property anymore.

P.S.: https://github/microsoftgraph/msgraph-sdk-dotnet/discussions/2864

Share Improve this question edited Mar 17 at 14:08 23W asked Mar 17 at 10:40 23W23W 1,54022 silver badges47 bronze badges 6
  • Could you please share the screenshot of your error message and edit the question? – Pratik Jadhav Commented Mar 17 at 11:00
  • @PratikJadhav, nothing to share, source code client.Drives[someDriveId].Root.Children can not be compiled because Root request builder does not contain Children property. – 23W Commented Mar 17 at 11:02
  • 1 Microsoft Graph doesnot provide direct direct .Children property under Root. That's why this won't compile await client.Drives[someDriveId].Root.Children.PostAsync(newFolderItem); So as a workaround use ItemWithPath("/")directly targets the root of the OneDrive. – Pratik Jadhav Commented Mar 17 at 11:09
  • 1 @PratikJadhav, it is strange to see such workaround, but i agree that it could be an answer. please add this recomendation as the answer – 23W Commented Mar 17 at 11:24
  • Sure, we'll post an answer! – Pratik Jadhav Commented Mar 17 at 11:25
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Instead of .Root , you can use .Items["root"]. The latter has a Children property. This is called out in the documentation on listing the children of a drive item.

Here's an example:

var requestBody = new DriveItem
{
    Name = "Folder C",
    Folder = new Folder
    {
    },
    AdditionalData = new Dictionary<string, object>
    {
        {
            "@microsoft.graph.conflictBehavior" , "rename"
        },
    },
};

var item = await graphClient.Drives[driveId]
    .Items["root"]
    .Children
    .PostAsync(requestBody);

本文标签: azureHow to create a new folder in Drive root via MsGraph SDK for CStack Overflow