Search code examples
powershellmicrosoft-graph-apionedrivemicrosoft-graph-files

OneDrive FB create Folder with Microsoft Graph API Powershell


I just want to create a folder one my OneDrive for Buiness Account through the Graph API. After many hours I got stuck with an error which I really can't comprehend. It says that the property 'Attributes' does not exist on type 'oneDrive.folder' and I should not use this property. The problem is that I don't use this property. After alot of research I think it has something to do with the pinned metadata or something like that. But in gerneral I really dont know what to do futher.

I used the Graph Explorer and another website to create this script.

The Error:

-1, Microsoft.SharePoint.Client.InvalidClientQueryException 
The property 'Attributes' does not exist on type 'oneDrive.folder'.
Make sure to only use property names that are defined by the type.

And this is my code:

$clientId = "XXXXXXXXXXXXXX"
$tenantId = "XXXXXX.onmicrosoft.com"
$clientSecret = 'XXXXXXXXXXXX'


$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"


$body = @{
    client_id     = $clientId
    scope         = "https://graph.microsoft.com/.default"
    client_secret = $clientSecret
    grant_type    = "client_credentials"
}

$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
Write-Host $token


$uri = "https://graph.microsoft.com/v1.0/<ID XXXXXXX>/drive/root/children"
$method = 'POST'
$head= @{Authorization = "Bearer $token"} 
$postPara= @{
name= "NewFolder"
folder= {}
} | ConvertTo-Json


$antwort = Invoke-RestMethod -Headers $head -Uri $uri -Method $method -Body $postPara -ContentType "application/json"
Write-Host $antwort

It really should work and I'm sitting on this sample task for over 10 Hours ._.


Solution

  • The issue with you code is

    $postPara= @{
    name= "NewFolder"
    folder = {}
    } | ConvertTo-Json
    

    If you just output $postPara you will see the issue is because your missing the @ in front of value in folder you will actually get the details from the underlying script populated in there. so try

    $postPara= @{
    name= "NewFolder"
    folder = @{}
    } | ConvertTo-Json
    $postPara
    

    Which should fix it.A good diag tool is also to use fiddler to look at the request being sent to server.