powershellsharepointmicrosoft-graph-apisharepoint-onlinesharepointdocumentlibrary

Using PowerShell to call the GraphAPI and create a folder in a SharePoint document library


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

Solution

  • 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"
    }