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 | * Create List Item |
52 | * Add an item to a list. `fields` maps column internal names (see List Columns) to values, e.g. `{ "Title": "Widget", "Quantity": 3 }`. Requires Sites.ReadWrite.All (a connection default). |
53 | */ |
54 | export async function main( |
55 | auth: RT.MsSharepoint, |
56 | site_id: DynSelect_site_id, |
57 | list_id: DynSelect_list_id, |
58 | fields: { [key: string]: any } |
59 | ) { |
60 | const response = await fetch( |
61 | `${graph}/sites/${site_id}/lists/${list_id}/items`, |
62 | { |
63 | method: "POST", |
64 | headers: { |
65 | Authorization: `Bearer ${auth.token}`, |
66 | "Content-Type": "application/json", |
67 | Accept: "application/json", |
68 | }, |
69 | body: JSON.stringify({ fields }), |
70 | } |
71 | ) |
72 |
|
73 | if (!response.ok) { |
74 | throw new Error(`${response.status} ${await response.text()}`) |
75 | } |
76 |
|
77 | return await response.json() |
78 | } |
79 |
|