0

List Channel Members

by
Published today

List the members of a channel with their roles, user ids and email addresses. Requires ChannelMember.Read.All, which needs admin consent and is not a connection default: tick it when connecting.

Script ms_teams Verified

The script

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

3
export type DynSelect_team_id = string
4

5
// Dropdown of the teams the connected user has joined.
6
export async function team_id(auth: RT.MsTeams) {
7
  const response = await fetch(
8
    `https://graph.microsoft.com/v1.0/me/joinedTeams`,
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { value } = (await response.json()) as { value: any[] }
20
  return value.map((x) => ({ value: x.id, label: x.displayName }))
21
}
22

23
export type DynSelect_channel_id = string
24

25
// Dropdown of the chosen team's channels.
26
export async function channel_id(auth: RT.MsTeams, team_id: string) {
27
  if (team_id == null || team_id === "") return []
28
  const response = await fetch(
29
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels`,
30
    {
31
      headers: {
32
        Authorization: `Bearer ${auth.token}`,
33
        Accept: "application/json",
34
      },
35
    }
36
  )
37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40
  const { value } = (await response.json()) as { value: any[] }
41
  return value.map((x) => ({ value: x.id, label: x.displayName }))
42
}
43

44
/**
45
 * List Channel Members
46
 * List the members of a channel with their roles, user ids and email addresses. Requires ChannelMember.Read.All, which needs admin consent and is not a connection default: tick it when connecting.
47
 */
48
export async function main(
49
  auth: RT.MsTeams,
50
  team_id: DynSelect_team_id,
51
  channel_id: DynSelect_channel_id
52
) {
53
  const url = new URL(
54
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/channels/${encodeURIComponent(channel_id)}/members`
55
  )
56

57
  const response = await fetch(url, {
58
    headers: {
59
      Authorization: `Bearer ${auth.token}`,
60
      Accept: "application/json",
61
    },
62
  })
63

64
  if (!response.ok) {
65
    throw new Error(`${response.status} ${await response.text()}`)
66
  }
67

68
  return await response.json()
69
}
70