0

Update Calendar Event

by
Published today

Update an event, or one occurrence of a recurring event when given an occurrence id from List Event Instances. Only the fields you set are changed; `attendees` replaces the whole attendee list, and `start`/`end` are local times in `time_zone`. `extra` takes any other event property (e.g. `recurrence`, `isOnlineMeeting`). Requires Calendars.ReadWrite (a connection default).

Script ms_outlook Verified

The script

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

3
/**
4
 * Update Calendar Event
5
 * Update an event, or one occurrence of a recurring event when given an occurrence id from List Event Instances. Only the fields you set are changed; `attendees` replaces the whole attendee list, and `start`/`end` are local times in `time_zone`. `extra` takes any other event property (e.g. `recurrence`, `isOnlineMeeting`). Requires Calendars.ReadWrite (a connection default).
6
 */
7
export async function main(
8
  auth: RT.MsOutlook,
9
  event_id: string,
10
  subject: string | undefined,
11
  start: string | undefined,
12
  end: string | undefined,
13
  time_zone: string = "UTC",
14
  body: string | undefined,
15
  content_type: "HTML" | "Text" = "HTML",
16
  location: string | undefined,
17
  attendees: string[] | undefined,
18
  extra: { [key: string]: any } | undefined
19
) {
20
  const event: { [key: string]: any } = { ...(extra ?? {}) }
21
  if (subject != null && subject !== "") event.subject = subject
22
  if (start != null && start !== "") {
23
    event.start = { dateTime: start, timeZone: time_zone }
24
  }
25
  if (end != null && end !== "") {
26
    event.end = { dateTime: end, timeZone: time_zone }
27
  }
28
  if (body != null && body !== "") {
29
    event.body = { contentType: content_type, content: body }
30
  }
31
  if (location != null && location !== "") {
32
    event.location = { displayName: location }
33
  }
34
  if (attendees?.length) {
35
    event.attendees = attendees.map((address) => ({
36
      emailAddress: { address },
37
      type: "required",
38
    }))
39
  }
40

41
  const response = await fetch(
42
    `https://graph.microsoft.com/v1.0/me/events/${encodeURIComponent(event_id)}`,
43
    {
44
      method: "PATCH",
45
      headers: {
46
        Authorization: `Bearer ${auth.token}`,
47
        "Content-Type": "application/json",
48
        Accept: "application/json",
49
      },
50
      body: JSON.stringify(event),
51
    }
52
  )
53

54
  if (!response.ok) {
55
    throw new Error(`${response.status} ${await response.text()}`)
56
  }
57

58
  return await response.json()
59
}
60