0

Update Contact

by
Published today

Update a contact; only the fields you set are changed. `email_addresses` and `business_phones` replace the existing lists. `extra` takes any other contact property (e.g. `companyName`, `jobTitle`, `mobilePhone`). Requires Contacts.ReadWrite (a connection default).

Script ms_outlook Verified

The script

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

3
/**
4
 * Update Contact
5
 * Update a contact; only the fields you set are changed. `email_addresses` and `business_phones` replace the existing lists. `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.MsOutlook,
9
  contact_id: string,
10
  given_name: string | undefined,
11
  surname: string | undefined,
12
  email_addresses: string[] | undefined,
13
  business_phones: string[] | undefined,
14
  extra: { [key: string]: any } | undefined
15
) {
16
  const body: { [key: string]: any } = { ...(extra ?? {}) }
17
  if (given_name != null && given_name !== "") body.givenName = given_name
18
  if (surname != null && surname !== "") body.surname = surname
19
  if (email_addresses?.length) {
20
    body.emailAddresses = email_addresses.map((address) => ({ address }))
21
  }
22
  if (business_phones?.length) body.businessPhones = business_phones
23

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

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  return await response.json()
42
}
43