0

Create Contact

by
Published today

Add a contact to the connected account's default contacts folder. `extra` takes any other contact property (e.g. `companyName`, `jobTitle`, `mobilePhone`). Requires Contacts.ReadWrite (a connection default).

Script outlook Verified

The script

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

3
/**
4
 * Create Contact
5
 * Add a contact to the connected account's default contacts folder. `extra` takes any other contact property (e.g. `companyName`, `jobTitle`, `mobilePhone`). Requires Contacts.ReadWrite (a connection default).
6
 */
7
export async function main(
8
  auth: RT.Outlook,
9
  given_name: string,
10
  surname: string | undefined,
11
  email_addresses: string[] | undefined,
12
  business_phones: string[] | undefined,
13
  extra: { [key: string]: any } | undefined
14
) {
15
  const body: { [key: string]: any } = { ...(extra ?? {}) }
16
  if (given_name != null && given_name !== "") body.givenName = given_name
17
  if (surname != null && surname !== "") body.surname = surname
18
  if (email_addresses?.length) {
19
    body.emailAddresses = email_addresses.map((address) => ({ address }))
20
  }
21
  if (business_phones?.length) body.businessPhones = business_phones
22

23
  const response = await fetch("https://graph.microsoft.com/v1.0/me/contacts", {
24
    method: "POST",
25
    headers: {
26
      Authorization: `Bearer ${auth.token}`,
27
      "Content-Type": "application/json",
28
      Accept: "application/json",
29
    },
30
    body: JSON.stringify(body),
31
  })
32

33
  if (!response.ok) {
34
    throw new Error(`${response.status} ${await response.text()}`)
35
  }
36

37
  return await response.json()
38
}
39