0

List Lists

by
Published today

List a site's lists, document libraries included (a library is a list whose `list.template` is `documentLibrary`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).

Script ms_sharepoint Verified

The script

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

3
const graph = "https://graph.microsoft.com/v1.0"
4

5
export type DynSelect_site_id = string
6

7
export async function site_id(auth: RT.MsSharepoint) {
8
  const response = await fetch(
9
    `${graph}/sites?search=*&$select=id,displayName,webUrl&$top=200`,
10
    {
11
      headers: {
12
        Authorization: `Bearer ${auth.token}`,
13
        Accept: "application/json",
14
      },
15
    }
16
  )
17
  if (!response.ok) {
18
    throw new Error(`${response.status} ${await response.text()}`)
19
  }
20
  const { value } = (await response.json()) as {
21
    value: { id: string; displayName: string; webUrl: string }[]
22
  }
23
  return value.map((s) => ({
24
    value: s.id,
25
    label: `${s.displayName} (${s.webUrl})`,
26
  }))
27
}
28

29
/**
30
 * List Lists
31
 * List a site's lists, document libraries included (a library is a list whose `list.template` is `documentLibrary`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
32
 */
33
export async function main(
34
  auth: RT.MsSharepoint,
35
  site_id: DynSelect_site_id,
36
  select: string | undefined,
37
  top: number = 100,
38
  next_link: string | undefined
39
) {
40
  if (
41
    next_link != null &&
42
    next_link !== "" &&
43
    !next_link.startsWith("https://graph.microsoft.com/")
44
  ) {
45
    throw new Error(
46
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
47
    )
48
  }
49
  let url: URL
50
  if (next_link != null && next_link !== "") {
51
    url = new URL(next_link)
52
  } else {
53
    url = new URL(`${graph}/sites/${site_id}/lists`)
54
    url.searchParams.append("$top", String(top))
55
    if (select != null && select !== "") {
56
      url.searchParams.append("$select", select)
57
    }
58
  }
59

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

67
  if (!response.ok) {
68
    throw new Error(`${response.status} ${await response.text()}`)
69
  }
70

71
  return await response.json()
72
}
73