0

List Folders

by
Published today

List mail folders with their unread and total counts: the top level, or the children of `parent_folder` (a folder id or well-known name such as `inbox`). Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).

Script ms_outlook Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Folders
5
 * List mail folders with their unread and total counts: the top level, or the children of `parent_folder` (a folder id or well-known name such as `inbox`). Requires Mail.Read (covered by the default Mail.ReadWrite) (a shared mailbox needs Mail.Read.Shared, not a default).
6
 */
7
export async function main(
8
  auth: RT.MsOutlook,
9
  parent_folder: string | undefined,
10
  top: number = 100,
11
  next_link: string | undefined,
12
  mailbox: string | undefined
13
) {
14
  const base = `https://graph.microsoft.com/v1.0/${
15
    mailbox != null && mailbox !== ""
16
      ? `users/${encodeURIComponent(mailbox)}`
17
      : "me"
18
  }`
19
  if (
20
    next_link != null &&
21
    next_link !== "" &&
22
    !next_link.startsWith("https://graph.microsoft.com/")
23
  ) {
24
    throw new Error(
25
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
26
    )
27
  }
28
  let url: URL
29
  if (next_link != null && next_link !== "") {
30
    url = new URL(next_link)
31
  } else {
32
    url = new URL(
33
      parent_folder != null && parent_folder !== ""
34
        ? `${base}/mailFolders/${encodeURIComponent(parent_folder)}/childFolders`
35
        : `${base}/mailFolders`
36
    )
37
    url.searchParams.append("$top", String(top))
38
  }
39

40
  const response = await fetch(url, {
41
    headers: {
42
      Authorization: `Bearer ${auth.token}`,
43
      Accept: "application/json",
44
    },
45
  })
46

47
  if (!response.ok) {
48
    throw new Error(`${response.status} ${await response.text()}`)
49
  }
50

51
  return await response.json()
52
}
53