AlanSTACK
AlanSTACK

Reputation: 6065

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?

Upvotes: 4

Views: 8045

Answers (4)

Bharvi Manvar
Bharvi Manvar

Reputation: 49

You can get all mail folder lists in Microsoft graph API using CURL

$curl = curl_init();
$accessToken = ""; //Get the access token from your database or txt file
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://graph.microsoft.com/v1.0/me/mailFolders/?includeHiddenFolders=true',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer '
  ),
 ));
   $response = curl_exec($curl);
   curl_close($curl);
   echo $response;

Upvotes: 2

SKU123
SKU123

Reputation: 81

Yes, you can. Simply use delta query to get all the folders.

Request Example: https://graph.microsoft.com/v1.0/users/[user_id]/mailfolders/delta?$select=displayname You get an array of all the folders, with child folders just after the parent-folder item in the response.

Test:
Go to : https://developer.microsoft.com/en-us/graph/graph-explorer
GET Version: v1.0 URL: https://graph.microsoft.com/v1.0/me/Mailfolders/delta
Run Query

Notice "Internal Screens" and "Project Falcon" folders whose parentFolderId is the ID of "Inbox" are also included in the response.

Upvotes: 8

factorypolaris
factorypolaris

Reputation: 2847

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
  }
]

Upvotes: 9

Jason Johnston
Jason Johnston

Reputation: 17692

No there isn't. You should check the childFolderCount property on each folder and only query if non-zero.

Upvotes: 2

Related Questions