Edits history of script submission #23350 for ' Move Email to Folder (outlook)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_destination_folder = string
    
    export async function destination_folder(
      auth: RT.Outlook,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { value } = (await r.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((f) => ({ value: f.id, label: f.displayName }))
    }
    
    /**
     * Move Email to Folder
     * Move a message to another folder: a folder id, or a well-known name such as `inbox`, `archive`, `deleteditems`, `drafts`, `junkemail` or `sentitems`. The moved message gets a new id, returned in the response. Requires Mail.ReadWrite (a connection default).
     */
    export async function main(
      auth: RT.Outlook,
      message_id: string,
      destination_folder: DynSelect_destination_folder,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const response = await fetch(
        `${base}/messages/${encodeURIComponent(message_id)}/move`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ destinationId: destination_folder }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago

  • bunnative
    //native
    
    export type DynSelect_destination_folder = string
    
    export async function destination_folder(
      auth: RT.Outlook,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const r = await fetch(`${base}/mailFolders?$top=100&$select=id,displayName`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
      const { value } = (await r.json()) as {
        value: { id: string; displayName: string }[]
      }
      return value.map((f) => ({ value: f.id, label: f.displayName }))
    }
    
    /**
     * Move Email to Folder
     * Move a message to another folder: a folder id, or a well-known name such as `inbox`, `archive`, `deleteditems`, `drafts`, `junkemail` or `sentitems`. The moved message gets a new id, returned in the response. Requires Mail.ReadWrite on the connection.
     */
    export async function main(
      auth: RT.Outlook,
      message_id: string,
      destination_folder: DynSelect_destination_folder,
      mailbox: string | undefined
    ) {
      const base = `https://graph.microsoft.com/v1.0/${
        mailbox != null && mailbox !== ""
          ? `users/${encodeURIComponent(mailbox)}`
          : "me"
      }`
      const response = await fetch(
        `${base}/messages/${encodeURIComponent(message_id)}/move`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({ destinationId: destination_folder }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 6 hours ago