0

Search Files

by
Published today

Search files and folders by name, metadata and content in a site's default document library, or in another of its libraries when `drive_id` is set. Results come from the search index, so a file created seconds ago may not appear yet. Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).

Script ms_sharepoint 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
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
/**
58
 * Search Files
59
 * Search files and folders by name, metadata and content in a site's default document library, or in another of its libraries when `drive_id` is set. Results come from the search index, so a file created seconds ago may not appear yet. Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
60
 */
61
export async function main(
62
  auth: RT.MsSharepoint,
63
  site_id: DynSelect_site_id,
64
  query: string,
65
  select: string | undefined,
66
  top: number = 50,
67
  next_link: string | undefined,
68
  drive_id: DynSelect_drive_id | undefined
69
) {
70
  const drive = driveUrl(site_id, drive_id)
71
  if (
72
    next_link != null &&
73
    next_link !== "" &&
74
    !next_link.startsWith("https://graph.microsoft.com/")
75
  ) {
76
    throw new Error(
77
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
78
    )
79
  }
80
  let url: URL
81
  if (next_link != null && next_link !== "") {
82
    url = new URL(next_link)
83
  } else {
84
    url = new URL(
85
      `${drive}/root/search(q='${encodeURIComponent(query.replace(/'/g, "''"))}')`
86
    )
87
    url.searchParams.append("$top", String(top))
88
    if (select != null && select !== "") {
89
      url.searchParams.append("$select", select)
90
    }
91
  }
92

93
  const response = await fetch(url, {
94
    headers: {
95
      Authorization: `Bearer ${auth.token}`,
96
      Accept: "application/json",
97
    },
98
  })
99

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

104
  return await response.json()
105
}
106