Edits history of script submission #23538 for ' List List Items (ms_sharepoint)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    const graph = "https://graph.microsoft.com/v1.0"
    
    export type DynSelect_site_id = string
    
    export async function site_id(auth: RT.MsSharepoint) {
      const response = await fetch(
        `${graph}/sites?search=*&$select=id,displayName,webUrl&$top=200`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { value } = (await response.json()) as {
        value: { id: string; displayName: string; webUrl: string }[]
      }
      return value.map((s) => ({
        value: s.id,
        label: `${s.displayName} (${s.webUrl})`,
      }))
    }
    
    export type DynSelect_list_id = string
    
    export async function list_id(auth: RT.MsSharepoint, site_id: string) {
      const response = await fetch(
        `${graph}/sites/${site_id}/lists?$select=id,displayName`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { value } = (await response.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((l) => ({ value: l.id, label: l.displayName }))
    }
    
    /**
     * List List Items
     * List or filter the items of a list with their column values; on a document library's list each item is a file with its metadata. `filter` takes OData on columns (e.g. `fields/Status eq 'Open'`), `fields` limits the returned columns (comma-separated internal names). Filtering or sorting on a column that is not indexed fails unless `allow_unindexed` is set, which is slow on large lists. Pass the returned `@odata.nextLink` as `next_link` for the next page. Requires Sites.Read.All (covered by the default Sites.ReadWrite.All).
     */
    export async function main(
      auth: RT.MsSharepoint,
      site_id: DynSelect_site_id,
      list_id: DynSelect_list_id,
      filter: string | undefined,
      order_by: string | undefined,
      fields: string | undefined,
      allow_unindexed: boolean = false,
      top: number = 100,
      next_link: string | undefined
    ) {
      if (
        next_link != null &&
        next_link !== "" &&
        !next_link.startsWith("https://graph.microsoft.com/")
      ) {
        throw new Error(
          "next_link must be an @odata.nextLink returned by Microsoft Graph"
        )
      }
      let url: URL
      if (next_link != null && next_link !== "") {
        url = new URL(next_link)
      } else {
        url = new URL(`${graph}/sites/${site_id}/lists/${list_id}/items`)
        url.searchParams.append("$top", String(top))
        url.searchParams.append(
          "$expand",
          fields != null && fields !== "" ? `fields($select=${fields})` : "fields"
        )
        if (filter != null && filter !== "") {
          url.searchParams.append("$filter", filter)
        }
        if (order_by != null && order_by !== "") {
          url.searchParams.append("$orderby", order_by)
        }
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
          ...(allow_unindexed
            ? { Prefer: "HonorNonIndexedQueriesWarningMayFailRandomly" }
            : {}),
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago