0

List Portfolios

by
Published Oct 31, 2023

List the portfolios in a workspace owned by a user (`me` by default). Portfolios are available on Asana's paid business plans. Pass `next_page.offset` from a response as `offset` to get the next page.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_workspace = string
4

5
// Resolver: the workspaces and organizations the token can access.
6
export async function workspace(auth: RT.Asana) {
7
  const response = await fetch(
8
    "https://app.asana.com/api/1.0/workspaces?limit=100",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { data } = await response.json()
20
  return data.map((w: { gid: string; name: string }) => ({
21
    value: w.gid,
22
    label: w.name,
23
  }))
24
}
25

26
/**
27
 * List Portfolios
28
 * List the portfolios in a workspace owned by a user (`me` by default). Portfolios are available on Asana's paid business plans. Pass `next_page.offset` from a response as `offset` to get the next page.
29
 */
30
export async function main(
31
  auth: RT.Asana,
32
  workspace: DynSelect_workspace,
33
  owner: string | undefined,
34
  opt_fields: string | undefined,
35
  limit: number | undefined,
36
  offset: string | undefined
37
) {
38
  const url = new URL(`https://app.asana.com/api/1.0/portfolios`)
39
  const params = {
40
    workspace,
41
    opt_fields,
42
    owner: owner || "me",
43
    limit: limit ?? 100,
44
    offset,
45
  }
46
  for (const [key, value] of Object.entries(params)) {
47
    if (value != null && value !== "") {
48
      url.searchParams.append(key, String(value))
49
    }
50
  }
51

52
  const response = await fetch(url, {
53
    headers: {
54
      Authorization: `Bearer ${auth.token}`,
55
      Accept: "application/json",
56
    },
57
  })
58

59
  if (!response.ok) {
60
    throw new Error(`${response.status} ${await response.text()}`)
61
  }
62

63
  return await response.json()
64
}
65

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get multiple portfolios
    6
     * Returns a list of the portfolios in compact representation that are owned by the current API user.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      opt_pretty: string | undefined,
    11
      opt_fields: string | undefined,
    12
      limit: string | undefined,
    13
      offset: string | undefined,
    14
      workspace: string | undefined,
    15
      owner: string | undefined
    16
    ) {
    17
      const url = new URL(`https://app.asana.com/api/1.0/portfolios`);
    18
      for (const [k, v] of [
    19
        ["opt_pretty", opt_pretty],
    20
        ["opt_fields", opt_fields],
    21
        ["limit", limit],
    22
        ["offset", offset],
    23
        ["workspace", workspace],
    24
        ["owner", owner],
    25
      ]) {
    26
        if (v !== undefined && v !== "") {
    27
          url.searchParams.append(k, v);
    28
        }
    29
      }
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          Authorization: "Bearer " + auth.token,
    34
        },
    35
        body: undefined,
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43