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. `embed` links exist only on personal OneDrive accounts. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.

Script ms_onedrive Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
const graph = "https://graph.microsoft.com/v1.0"
4

5
function driveUrl(drive_id: string | undefined) {
6
  return drive_id != null && drive_id !== ""
7
    ? `${graph}/drives/${encodeURIComponent(drive_id)}`
8
    : `${graph}/me/drive`
9
}
10

11
// An item is addressed by its id, or by a path from the drive root starting with "/"
12
function ref(item: string | undefined) {
13
  if (item == null || item === "" || item === "/") return "root"
14
  if (!item.startsWith("/")) return `items/${encodeURIComponent(item)}`
15
  const path = item
16
    .replace(/\/+$/, "")
17
    .split("/")
18
    .map(encodeURIComponent)
19
    .join("/")
20
  return `root:${path}:`
21
}
22

23
/**
24
 * Create Sharing Link
25
 * 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. `embed` links exist only on personal OneDrive accounts. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.
26
 */
27
export async function main(
28
  auth: RT.MsOnedrive,
29

30
  item: string,
31
  type: "view" | "edit" | "embed" = "view",
32
  scope: "anonymous" | "organization" | "users" | undefined,
33
  expiration_date_time: string | undefined,
34
  drive_id: string | undefined
35
) {
36
  const drive = driveUrl(drive_id)
37
  const body: { [key: string]: any } = { type }
38
  if (scope != null && (scope as string) !== "") body.scope = scope
39
  if (expiration_date_time != null && expiration_date_time !== "") {
40
    body.expirationDateTime = expiration_date_time
41
  }
42

43
  const response = await fetch(`${drive}/${ref(item)}/createLink`, {
44
    method: "POST",
45
    headers: {
46
      Authorization: `Bearer ${auth.token}`,
47
      "Content-Type": "application/json",
48
      Accept: "application/json",
49
    },
50
    body: JSON.stringify(body),
51
  })
52

53
  if (!response.ok) {
54
    throw new Error(`${response.status} ${await response.text()}`)
55
  }
56

57
  return await response.json()
58
}
59