Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft graph - outlook mail - list all mail folders (not just the top level ones)?

Currently, as far as I can tell, Microsoft graph offers 2 primary endpoints for outlook mail folders according to https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/message

List mail folders and List child folders - meaning just to build the folder hierarchy in my dashboard app, I need to make recursive REST API calls - which is very slow and very bad.

Is there any way to get microsoft to just return all folders at once?

like image 717
AlanSTACK Avatar asked Sep 15 '25 04:09

AlanSTACK


1 Answers

Using the current V1.0

You can get two levels per call by expanding the childFolders container.

https://graph.microsoft.com/v1.0/me/mailFolders?$top=250&$expand=childFolders

Using the Beta Version

You can fetch up to 250 folders per request, you'll receive a flattened hierarchy which includes unlimited child levels. The beta version has been in beta since 2015 (best I can tell) and I cannot say when/if it will move to production. In my case I use the beta for fetching folders and fallback to v1.0 and recursive looping.

Simply change your GET request slightly. v1.0 to beta. For me, the existing Auth tokens and credentials all work without any changes. Also note, there is no need to expand the childFolders using the Beta endpoint.

https://graph.microsoft.com/beta/me/mailFolders?$top=250

Example Response...

[{
    "id": "xx-1",
    "displayName": "Inbox",
    "parentFolderId": "xx-0",
    "childFolderCount": 1,
    "unreadItemCount": 8,
    "totalItemCount": 22,
    "wellKnownName": "inbox"
  },
  {
    "id": "xx-2",
    "displayName": "Level 2",
    "parentFolderId": "xx-1",
    "childFolderCount": 1,
    "unreadItemCount": 2,
    "totalItemCount": 4,
    "wellKnownName": null
  },
  {
    "id": "xx-3",
    "displayName": "level 3",
    "parentFolderId": "xx-2",
    "childFolderCount": 1,
    "unreadItemCount": 0,
    "totalItemCount": 0,
    "wellKnownName": null
  },
  {
    "id": "xx-4",
    "displayName": "level 4",
    "parentFolderId": "xx-3",
    "childFolderCount": 0,
    "unreadItemCount": 0,
    "totalItemCount": 0,
    "wellKnownName": null
  }
]
like image 157
factorypolaris Avatar answered Sep 16 '25 18:09

factorypolaris