0

List Team Members

by
Published today

List the members of a team with their roles, user ids and email addresses. Requires TeamMember.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 1 hour 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
/**
24
 * List Team Members
25
 * List the members of a team with their roles, user ids and email addresses. Requires TeamMember.Read.All, which needs admin consent and is not a connection default: tick it when connecting.
26
 */
27
export async function main(auth: RT.MsTeams, team_id: DynSelect_team_id) {
28
  const url = new URL(
29
    `https://graph.microsoft.com/v1.0/teams/${encodeURIComponent(team_id)}/members`
30
  )
31

32
  const response = await fetch(url, {
33
    headers: {
34
      Authorization: `Bearer ${auth.token}`,
35
      Accept: "application/json",
36
    },
37
  })
38

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

43
  return await response.json()
44
}
45