Edits history of script submission #23542 for ' Search Files Across Sites (ms_sharepoint)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    const graph = "https://graph.microsoft.com/v1.0"
    
    /**
     * Search Files Across Sites
     * Search every site and OneDrive the connected account can reach with Microsoft Search. `query` takes KQL (e.g. `budget filetype:xlsx`, `path:"https://contoso.sharepoint.com/sites/Finance"`). `entity_types` picks what to return: `driveItem` (files and folders), `listItem`, `list`, `site` or `drive`. Hits are under `value[0].hitsContainers[0].hits`; page with `from`. Work or school accounts only. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
     */
    export async function main(
      auth: RT.MsSharepoint,
    
      query: string,
      entity_types: ("driveItem" | "listItem" | "list" | "site" | "drive")[] = [
        "driveItem",
      ],
      fields: string[] | undefined,
      size: number = 25,
      from: number = 0
    ) {
      const request: { [key: string]: any } = {
        entityTypes: entity_types?.length ? entity_types : ["driveItem"],
        query: { queryString: query },
        from,
        size,
      }
      if (fields?.length) request.fields = fields
    
      const response = await fetch(`${graph}/search/query`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ requests: [request] }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago