Edits history of script submission #23526 for ' Create List (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})`,
      }))
    }
    
    /**
     * Create List
     * Create a list in a site. `template` is a SharePoint list template such as `genericList`, `documentLibrary`, `events` or `tasks`. `columns` takes Graph column definitions, e.g. `[{ "name": "Author", "text": {} }, { "name": "PageCount", "number": {} }]`; a Title column always exists. Requires Sites.Manage.All, not a connection default: tick it when connecting.
     */
    export async function main(
      auth: RT.MsSharepoint,
      site_id: DynSelect_site_id,
      display_name: string,
      description: string | undefined,
      template: string = "genericList",
      columns: { [key: string]: any }[] | undefined
    ) {
      const body: { [key: string]: any } = {
        displayName: display_name,
        list: { template },
      }
      if (description != null && description !== "") body.description = description
      if (columns?.length) body.columns = columns
    
      const response = await fetch(`${graph}/sites/${site_id}/lists`, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago