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_list_id = string |
30 |
|
31 | export async function list_id(auth: RT.MsSharepoint, site_id: string) { |
32 | const response = await fetch( |
33 | `${graph}/sites/${site_id}/lists?$select=id,displayName`, |
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; displayName: string }[] |
46 | } |
47 | return value.map((l) => ({ value: l.id, label: l.displayName })) |
48 | } |
49 |
|
50 | |
51 | * List List Items |
52 | * List or filter the items of a list with their column values; on a document library's list each item is a file with its metadata. `filter` takes OData on columns (e.g. `fields/Status eq 'Open'`), `fields` limits the returned columns (comma-separated internal names). Filtering or sorting on a column that is not indexed fails unless `allow_unindexed` is set, which is slow on large lists. Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All). |
53 | */ |
54 | export async function main( |
55 | auth: RT.MsSharepoint, |
56 | site_id: DynSelect_site_id, |
57 | list_id: DynSelect_list_id, |
58 | filter: string | undefined, |
59 | order_by: string | undefined, |
60 | fields: string | undefined, |
61 | allow_unindexed: boolean = false, |
62 | top: number = 100, |
63 | next_link: string | undefined |
64 | ) { |
65 | if ( |
66 | next_link != null && |
67 | next_link !== "" && |
68 | !next_link.startsWith("https://graph.microsoft.com/") |
69 | ) { |
70 | throw new Error( |
71 | "next_link must be an @odata.nextLink returned by Microsoft Graph" |
72 | ) |
73 | } |
74 | let url: URL |
75 | if (next_link != null && next_link !== "") { |
76 | url = new URL(next_link) |
77 | } else { |
78 | url = new URL(`${graph}/sites/${site_id}/lists/${list_id}/items`) |
79 | url.searchParams.append("$top", String(top)) |
80 | url.searchParams.append( |
81 | "$expand", |
82 | fields != null && fields !== "" ? `fields($select=${fields})` : "fields" |
83 | ) |
84 | if (filter != null && filter !== "") { |
85 | url.searchParams.append("$filter", filter) |
86 | } |
87 | if (order_by != null && order_by !== "") { |
88 | url.searchParams.append("$orderby", order_by) |
89 | } |
90 | } |
91 |
|
92 | const response = await fetch(url, { |
93 | headers: { |
94 | Authorization: `Bearer ${auth.token}`, |
95 | Accept: "application/json", |
96 | ...(allow_unindexed |
97 | ? { Prefer: "HonorNonIndexedQueriesWarningMayFailRandomly" } |
98 | : {}), |
99 | }, |
100 | }) |
101 |
|
102 | if (!response.ok) { |
103 | throw new Error(`${response.status} ${await response.text()}`) |
104 | } |
105 |
|
106 | return await response.json() |
107 | } |
108 |
|