0

Search Sites

by
Published today

Search the SharePoint sites the connected account can reach by name or description; an empty `search` lists them all. Results come from the search index, so a site created minutes ago may be missing: use Get Site for those. 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
/**
6
 * Search Sites
7
 * Search the SharePoint sites the connected account can reach by name or description; an empty `search` lists them all. Results come from the search index, so a site created minutes ago may be missing: use Get Site for those. Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
8
 */
9
export async function main(
10
  auth: RT.MsSharepoint,
11

12
  search: string | undefined,
13
  select: string | undefined,
14
  top: number = 100,
15
  next_link: string | undefined
16
) {
17
  if (
18
    next_link != null &&
19
    next_link !== "" &&
20
    !next_link.startsWith("https://graph.microsoft.com/")
21
  ) {
22
    throw new Error(
23
      "next_link must be an @odata.nextLink returned by Microsoft Graph"
24
    )
25
  }
26
  let url: URL
27
  if (next_link != null && next_link !== "") {
28
    url = new URL(next_link)
29
  } else {
30
    url = new URL(`${graph}/sites`)
31
    url.searchParams.append("$top", String(top))
32
    url.searchParams.append(
33
      "search",
34
      search != null && search !== "" ? search : "*"
35
    )
36
    if (select != null && select !== "") {
37
      url.searchParams.append("$select", select)
38
    }
39
  }
40

41
  const response = await fetch(url, {
42
    headers: {
43
      Authorization: `Bearer ${auth.token}`,
44
      Accept: "application/json",
45
    },
46
  })
47

48
  if (!response.ok) {
49
    throw new Error(`${response.status} ${await response.text()}`)
50
  }
51

52
  return await response.json()
53
}
54