Search code examples
sharepointmicrosoft-graph-apionedrive

Creating nested folder in Sharepoint with Graph API fails


I can create a nested folder /folder1/folder2 in (personal) OneDrive with the following Graph request (using the beta API):

POST drives/{id}/root:/folder1:/children

{
  "name": "folder2",
  "folder": {},
  "@microsoft.graph.conflictBehavior": "fail"
}

Here, folder1 didn't exist beforehand; the one API call creates both it and folder1/folder2.

However, the same code fails for SharePoint and OneDrive for Business with a 404 error. Is this type of request supported? I'd rather not have to make a separate API call for every level of nesting.

I'm asking this because I'm not sure whether my site has policies that cause the request to fail, or because the API actually doesn't support it.


Solution

  • This works for me. Remove name parameter from the body and use PATCH instead of POST. Specify the path in the URL.

    PATCH drives/{id}/root:/folder1/folder2
    
    {
        "folder": {},
        "@microsoft.graph.conflictBehavior": "fail"
    }
    

    Tested using v1.0 and beta for OneDrive for Business.