1 | |
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 | export type DynSelect_drive_id = string |
30 |
|
31 | export async function drive_id(auth: RT.MsSharepoint, site_id: string) { |
32 | const response = await fetch( |
33 | `${graph}/sites/${site_id}/drives?$select=id,name`, |
34 | { |
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 | const { value } = (await response.json()) as { |
45 | value: { id: string; name: string }[] |
46 | } |
47 | return value.map((d) => ({ value: d.id, label: d.name })) |
48 | } |
49 |
|
50 | |
51 | function driveUrl(site_id: string, drive_id: string | undefined) { |
52 | return drive_id != null && drive_id !== "" |
53 | ? `${graph}/drives/${encodeURIComponent(drive_id)}` |
54 | : `${graph}/sites/${site_id}/drive` |
55 | } |
56 |
|
57 | |
58 | function ref(item: string | undefined) { |
59 | if (item == null || item === "" || item === "/") return "root" |
60 | if (!item.startsWith("/")) return `items/${encodeURIComponent(item)}` |
61 | const path = item |
62 | .replace(/\/+$/, "") |
63 | .split("/") |
64 | .map(encodeURIComponent) |
65 | .join("/") |
66 | return `root:${path}:` |
67 | } |
68 |
|
69 | |
70 | * List Files in Folder |
71 | * List the files and folders directly inside a folder (the root when `folder` is empty) in a site's default document library, or in another of its libraries when `drive_id` is set. `folder` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `filter` and `order_by` take OData (e.g. `name desc`). Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All). |
72 | */ |
73 | export async function main( |
74 | auth: RT.MsSharepoint, |
75 | site_id: DynSelect_site_id, |
76 | folder: string | undefined, |
77 | order_by: string | undefined, |
78 | filter: string | undefined, |
79 | select: string | undefined, |
80 | top: number = 100, |
81 | next_link: string | undefined, |
82 | drive_id: DynSelect_drive_id | undefined |
83 | ) { |
84 | const drive = driveUrl(site_id, drive_id) |
85 | if ( |
86 | next_link != null && |
87 | next_link !== "" && |
88 | !next_link.startsWith("https://graph.microsoft.com/") |
89 | ) { |
90 | throw new Error( |
91 | "next_link must be an @odata.nextLink returned by Microsoft Graph" |
92 | ) |
93 | } |
94 | let url: URL |
95 | if (next_link != null && next_link !== "") { |
96 | url = new URL(next_link) |
97 | } else { |
98 | url = new URL(`${drive}/${ref(folder)}/children`) |
99 | url.searchParams.append("$top", String(top)) |
100 | if (order_by != null && order_by !== "") { |
101 | url.searchParams.append("$orderby", order_by) |
102 | } |
103 | if (filter != null && filter !== "") { |
104 | url.searchParams.append("$filter", filter) |
105 | } |
106 | if (select != null && select !== "") { |
107 | url.searchParams.append("$select", select) |
108 | } |
109 | } |
110 |
|
111 | const response = await fetch(url, { |
112 | headers: { |
113 | Authorization: `Bearer ${auth.token}`, |
114 | Accept: "application/json", |
115 | }, |
116 | }) |
117 |
|
118 | if (!response.ok) { |
119 | throw new Error(`${response.status} ${await response.text()}`) |
120 | } |
121 |
|
122 | return await response.json() |
123 | } |
124 |
|