question

orttt-3837 avatar image
0 Votes"
orttt-3837 asked orttt-3837 edited

Create mailbox folder with Graph API

I want to use the Graph API to create a folder in a mailbox that belongs to Exchange Online.
After getting the token, I was able to create a folder with the following command.
However, it cannot be created as a child folder of the specified folder.
Is it possible to create directly under the inbox?

$url = "https://graph.microsoft.com/v1.0/users/testuser01@domain.com/mailFolders/inbox"
$Body = @"
{
"displayName": "TestFolder",
"parentFolderId": "**"
}
"@

Invoke-RestMethod -Uri $url -Method Post -Body $Body -ContentType 'application/json; charset=utf-8' -Headers $headerParams | ConvertTo-Json

azure-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

soumi-MSFT avatar image
0 Votes"
soumi-MSFT answered orttt-3837 edited

@orttt-3837, I just tested this api and it worked for me. Let me share the detailed steps below:

  1. Use Authorization Code Grant Flow to fetch a token for your App from AAD.

  2. Once you have the code use it to request for an access_token from AAD. make sure the following delegated permission "Mail.ReadWrite" is present on the app registration that is being used here.

  3. Call the following Graph API.

API: https://graph.microsoft.com/v1.0/userPrincipalName/mailFolders/{id}/childFolders

Eg:
https://graph.microsoft.com/v1.0/me/mailFolders/AAMkADAwZmE5NGU1LWJlYzAtNGE0ZS1hZDI4LWZmNmU0MDdjNzgyZQAuAAAAAABh0v7KYQaiRob9eDRfUq1JAQDBRn5LIsmdTKmOCwZeGkgvAAAAAAEMAAA=/childFolders

Body:
{
"displayName": "{Child Folder Name}",
}

This is the ID for my inbox "AAMkADAwZmE5NGU1LWJlYzAtNGE0ZS1hZDI4LWZmNmU0MDdjNzgyZQAuAAAAAABh0v7KYQaiRob9eDRfUq1JAQDBRn5LIsmdTKmOCwZeGkgvAAAAAAEMAAA="

Once you make the api call, you should get status 200 OK and the child folder should be present in your outlook:


7413-outlook.png

Hope this helps.

Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as Answer; if the above response helped in answering your query.





outlook.png (23.8 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thank you for your answer.
I tried to execute it with the contents you received, but since the target mail folder ID contains "/", the ID is divided and recognized.
I specified it as'FolderID ', but since it causes an error, I would like to know if there is a workaround.
ID example:
LgAAAACCNU9IAzMUQ6jLAv0rEvlzAQA6GtvTEbRkRLs/dbSKyzqnAAAAAAEiAAAB

 $Mailbox = "testuser01@test.com"
 $FolderId = Get-MailboxFolderStatistics $Mailbox | ?{$_.Name -eq "Inbox"} | ForEach-Object{$_.FolderId}
    
 $url = "https://graph.microsoft.com/v1.0/users/$($Mailbox)/mailFolders/'$($FolderId)'/childFolders"
 $Body = @"
 {
     "displayName": "TestFolder"
 }
 "@
    
 Invoke-RestMethod -Uri $url -Method Post -Body $Body -ContentType 'application/json; charset=utf-8' -Headers $headerParams | ConvertTo-Json


0 Votes 0 ·

@orttt-3837, Sorry for the delay in response. As far as I understood, when you are running the Get-MailboxFolderStatistics its adding some extra characters before the FolderID. I guess to get things moving for you, you would have to somehow parse and remove the extra characters and get the actual Folder ID, or one more thing that you can do is make a graph api call to list the folders and then fetch the folder ID from there and feed it to the next instruction in your script.

API: GET https://graph.microsoft.com/v1.0/me/mailFolders

More info: https://docs.microsoft.com/en-us/graph/api/user-list-mailfolders?view=graph-rest-1.0&tabs=http


Hope this helps.

Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as Answer; if the above response helped in answering your query.

0 Votes 0 ·

@orttt-3837, Just wanted to followup with you to check if the above response helped in answering your query. If it did, please do not forget to accept the response as Answer; if the above response helped in answering your query. If it didn't, please do let us know so that we can try to help you further.

0 Votes 0 ·
Show more comments