0

List Document Libraries

by
Published today

List a site's document libraries (drives). Use a library's `id` as `drive_id` in the file scripts; without it they act on the site's default library. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).

Script ms_sharepoint Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 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 Document Libraries
31
 * List a site's document libraries (drives). Use a library's `id` as `drive_id` in the file scripts; without it they act on the site's default library. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
32
 */
33
export async function main(auth: RT.MsSharepoint, site_id: DynSelect_site_id) {
34
  const response = await fetch(`${graph}/sites/${site_id}/drives`, {
35
    headers: {
36
      Authorization: `Bearer ${auth.token}`,
37
      Accept: "application/json",
38
    },
39
  })
40

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

45
  return await response.json()
46
}
47