How to build post request to create the nested folder on site using updated graph api version 5.56.0.

Ritu Yadav 0 Reputation points
2024-09-12T06:12:58.0533333+00:00

I am try to create the POST request in c# using graph api version 5.56.0 but not able to create. I have try like

var graphClient = GetGraphServiceClient();
var result =  graphClient
.Sites[siteid]
.Drives[driveid]
.Root
.Children
.Request()
.AddAsync(folderItem);

but getting error "DriveItemRequestBuilder" does not contaion a defination for 'Root' and no accessible extension. same getting when try Item["Root"] or any other.

can you provide any way how solve this issue using graph api 5.56.0 version

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,273 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tiny Wang-MSFT 2,731 Reputation points Microsoft Vendor
    2024-09-12T09:35:21.63+00:00

    Hi @Ritu Yadav , your codes are used in graph api version 4.x. You might downgrade your Graph SDK version. If you want to use Graph SDK V5.x, then there's no .Request() indeed, you can see the migration guide here. The Drive Item path also changed in SDK, you might search for Drive and see the details.

    Creating nested folder should follow this section and the request should look like this.

    using Microsoft.Graph.Models; 
    
    var requestBody = new DriveItem { 
    	Name = "New Folder", 
    	Folder = new Folder { }, 
    	AdditionalData = new Dictionary<string, object> { 	
    		{ "@microsoft.graph.conflictBehavior" , "rename" }, 
    	}, 
    }; 
    // To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp 
    var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Children.PostAsync(requestBody);
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Tiny

    0 comments No comments

Your answer

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