skip to Main Content

I’m trying to use the Microsoft Graph Powershell module to create a a folder in a Sharepoint site’s drive.

Looking at the documentation here I should be able to do the following:

Import-Module Microsoft.Graph.Files

$params = @{
    name = "New Folder"
    folder = @{
    }
    "@microsoft.graph.conflictBehavior" = "rename"
}

New-MgDriveItemChild -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params

However, this is giving me an error:

New-MgDriveItemChild_Create: You must provide either a file facet if creating a file or a folder facet if creating a folder or a remoteItem facet if adding a shared folder

Status: 400 (BadRequest)
ErrorCode: invalidRequest
Date: 2023-11-23T17:50:50

I have got the driveitem ID of the subfolder from the site’s shared drive but it will not create the folder? I can use postman ok to create the folder directly via the API, but Powershell doesn’t seem to like it.

Maybe my misunderstanding, but what is meant by a folder facet?

2

Answers


  1. It seems to me like a bug in the PowerShell SDK and in case of empty folder property, the PowerShell SDK just ignores the property and doesn’t include it in the request body.

    As a workaround, add some fake property to folder. It will ensure that the folder property is included in the request body.

    Import-Module Microsoft.Graph.Files
    
    $params = @{
        name = "New Folder"
        folder = @{
            # fake property to ensures that PowerShell SDK will include folder facet
            x = 'x'
        }
        "@microsoft.graph.conflictBehavior" = "rename"
    }
    
    New-MgDriveItemChild -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
    

    I’ve reported the bug. Not sure how long it will take until they will fix it.

    Login or Signup to reply.
  2. The error occurs if missed including folder facet while creating folder using Microsoft Graph PowerShell modules.

    When I ran your script to create new folder without defining folder facet, I too same error as below:

    Import-Module Microsoft.Graph.Files
    
    $params = @{
        name = "New Folder"
        folder = @{
        }
        "@microsoft.graph.conflictBehavior" = "rename"
    }
    
    $driveId = "xxxxxxx"
    $driveItemId = "xxxxxxxx"
    
    New-MgDriveItemChild -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
    

    Response:

    enter image description here

    To resolve the error, you need to define at least one folder property like childCount or view or any property in the request body while creating folder with New-MgDriveItemChild cmdlet.

    When I ran below modified PowerShell script by including folder facet, I got response like this:

    Import-Module Microsoft.Graph.Files
    
    $params = @{
        name = "New Folder"
        folder = @{
            "childCount" = 1024
            "view" = @{
                "@odata.type" = "microsoft.graph.folderView"
            }
        }
        "@microsoft.graph.conflictBehavior" = "rename"
    }
    
    $driveId = "xxxxxxx"
    $driveItemId = "xxxxxxxx"
    
    New-MgDriveItemChild -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
    

    Response:

    enter image description here

    To confirm that, I checked the same in OneDrive where new folder is created successfully as below:

    enter image description here

    Reference:
    Folder – Microsoft Graph v1.0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search