1

So I am trying to create a new folder in a SharePoint library using Graph API. I can get the access token just fine, but whenever I send a request to create a new folder, I get (400) bad request. Here is my code, any help would be much appreciated.

#header containing access token
$header = @{
    Authorization = "Bearer " + $resp.access_token
}

$CreateFolderURL = "https://graph.microsoft.com/v1.0/drives/(Here I write the drive ID)/items/root/children"

$uploadFolderRequestBody = @{
name= "NewFolder"
folder = $null
"@microsoft.graph.conflictBehavior"= "rename"
} | ConvertTo-Json

Invoke-RestMethod -Headers $header -Method Post -Body $uploadFolderRequestBody -ContentType "application/json" -Uri $CreateFolderURL

1 Answer 1

1

Try to use folder = @{} instead of folder = $null.

It produces a json where "folder" : null but it should be "folder" : {}

$uploadFolderRequestBody = @{
name= "NewFolder"
folder = @{}
"@microsoft.graph.conflictBehavior"= "rename"
} | ConvertTo-Json

It will produce the expected json

{
    "name":  "NewFolder",
    "folder":  {},
    "@microsoft.graph.conflictBehavior":  "rename"
}
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it would be something small, that worked, thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.