0

Find Contacts

by
Published today

List the connected account's contacts, optionally narrowed by an OData `filter` (e.g. `startswith(displayName,'Al')` or `emailAddresses/any(a:a/address eq '[email protected]')`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Contacts.Read (covered by the default Contacts.ReadWrite).

Script ms_outlook Verified

The script

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

3
/**
4
 * Find Contacts
5
 * List the connected account's contacts, optionally narrowed by an OData `filter` (e.g. `startswith(displayName,'Al')` or `emailAddresses/any(a:a/address eq 'alice@example.com')`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Contacts.Read (covered by the default Contacts.ReadWrite).
6
 */
7
export async function main(
8
  auth: RT.MsOutlook,
9
  filter: string | undefined,
10
  order_by: string | undefined,
11
  select: string | undefined,
12
  top: number = 25,
13
  next_link: string | undefined
14
) {
15
  if (
16
    next_link != null &&
17
    next_link !== "" &&
18
    !next_link.startsWith("https://graph.microsoft.com/")
19
  ) {
20
    throw new Error(
21
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
22
    )
23
  }
24
  let url: URL
25
  if (next_link != null && next_link !== "") {
26
    url = new URL(next_link)
27
  } else {
28
    url = new URL("https://graph.microsoft.com/v1.0/me/contacts")
29
    url.searchParams.append("$top", String(top))
30
    if (filter != null && filter !== "") {
31
      url.searchParams.append("$filter", filter)
32
    }
33
    if (order_by != null && order_by !== "") {
34
      url.searchParams.append("$orderby", order_by)
35
    }
36
    if (select != null && select !== "") {
37
      url.searchParams.append("$select", select)
38
    }
39
  }
40

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

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

52
  return await response.json()
53
}
54