0

Create Sharing Link

by
Published today

Create a sharing link for a file or folder, or return the existing one of the same type and scope. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `scope` is who the link works for: `anonymous` (anyone, when the organization allows it), `organization` (signed-in members; work or school accounts only) or `users` (only the people the item is shared with); empty uses the organization's default. Requires Sites.ReadWrite.All (a connection default).

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
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
// The site's default document library, or the library given by drive_id
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
// An item is addressed by its id, or by a path from the drive root starting with "/"
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
 * Create Sharing Link
71
 * Create a sharing link for a file or folder, or return the existing one of the same type and scope. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). `scope` is who the link works for: `anonymous` (anyone, when the organization allows it), `organization` (signed-in members; work or school accounts only) or `users` (only the people the item is shared with); empty uses the organization's default. Requires Sites.ReadWrite.All (a connection default).
72
 */
73
export async function main(
74
  auth: RT.MsSharepoint,
75
  site_id: DynSelect_site_id,
76
  item: string,
77
  type: "view" | "edit" = "view",
78
  scope: "anonymous" | "organization" | "users" | undefined,
79
  expiration_date_time: string | undefined,
80
  drive_id: DynSelect_drive_id | undefined
81
) {
82
  const drive = driveUrl(site_id, drive_id)
83
  const body: { [key: string]: any } = { type }
84
  if (scope != null && (scope as string) !== "") body.scope = scope
85
  if (expiration_date_time != null && expiration_date_time !== "") {
86
    body.expirationDateTime = expiration_date_time
87
  }
88

89
  const response = await fetch(`${drive}/${ref(item)}/createLink`, {
90
    method: "POST",
91
    headers: {
92
      Authorization: `Bearer ${auth.token}`,
93
      "Content-Type": "application/json",
94
      Accept: "application/json",
95
    },
96
    body: JSON.stringify(body),
97
  })
98

99
  if (!response.ok) {
100
    throw new Error(`${response.status} ${await response.text()}`)
101
  }
102

103
  return await response.json()
104
}
105