Invalid Request Error When Creating A New DriveItem

Emre Turkis 1 Reputation point
2022-11-14T10:18:51.877+00:00

We're getting the following error when we try to create a new driveItem(file). I checked the documentation but everything seems fine. I'm not experienced with this API but can it be that something changed recently? It says the 'parentReference' was not allowed there but it clearly is, looking at the documentation of driveItem. We didn't change the code too. Any ideas?

Request:

POST https://graph.microsoft.com/v1.0/drives/{driveId}/items/{itemId}/children
SdkVersion : graph-java/v1.5.0

{ "file": {}, "name": "....pdf", "parentReference": { "driveId": "b!CI0Uhi...", "id": "01BRIXMT..." } }

Response:

400 : Bad Request
Transfer-Encoding : chunked
Strict-Transport-Security : max-age=31536000
Cache-Control : no-store, no-cache
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"South Central US","Slice":"E","Ring":"5","ScaleUnit":"005","RoleInstance":"SN4PEPF0000085A"}}
client-request-id : 535b6eab-c9ad-45a2-9bc9-7264c935014d
Vary : Accept-Encoding
request-id : 202ce588-4f71-436c-b9f2-28e95a572eda
Date : Fri, 11 Nov 2022 20:18:32 GMT
Content-Type : application/json

{ "error": { "code": "invalidRequest", "message": "The item cannot contain a parentReference", "innerError": { "date": "2022-11-11T20:18:33", "request-id": "202ce588-4f71-436c-b9f2-28e95a572eda", "client-request-id": "535b6eab-c9ad-45a2-9bc9-7264c935014d" } } }

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,678 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Srinivasa Rao Darna 6,696 Reputation points Microsoft Vendor
    2022-11-14T15:57:30.867+00:00

    Hi ,

    Using driveitem-post-children you can only create a new file with a simple request body.

    POST https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{item-id}/children  
    {  
        "name": "New_Doc1.dox",  
        "file": {},  
        "@microsoft.graph.conflictBehavior": "rename"  
    }  
    

    You don't have to use parentReference in request body as the same is defined with items/{item-id} suggesting creating file under folder with item-id.
    260070-image.png
    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


  2. Grzegorz Orda 46 Reputation points
    2022-11-17T10:21:52.447+00:00

    Looks like MS made some clean up when it comes to unnecessary properties.

    I had same issue with
    await _graphClient.Sites[SiteId].Drive.Root.Children.Request().AddAsync(driveItem);

    but when commented out ParentReferences stated work.

    261398-image.png

    In order to create folder with deeper structure it's necessary to use

    await _graphClient.Sites[SiteId].Drive.Root.ItemWithPath(rootFolder).Children.Request().AddAsync(driveItem);