You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Microsoft Graph API的$expand=childFolders时,如何获取100个子文件夹?

解决Microsoft Graph API返回子文件夹数量限制的问题

你遇到的问题是因为$top=100只作用于顶层邮件文件夹,而通过$expand获取的childFolders默认仍然采用10条的分页限制。要让子文件夹返回最多100条,需要在$expand里针对子文件夹单独设置$top参数。

修正后的API端点

将原端点中的$expand=childFolders替换为$expand=childFolders($top=100),完整端点如下:

https://graph.microsoft.com/v1.0/me/mailFolders?$top=100&$expand=childFolders($top=100)
  • $top=100:控制顶层邮件文件夹最多返回100个
  • childFolders($top=100):控制每个顶层文件夹下的子文件夹最多返回100个

修改后的完整调用代码

const headers = new Headers();
headers.append("Authorization", "Bearer " + accessToken);

// 修正后的请求URL
const response = await fetch(`https://graph.microsoft.com/v1.0/me/mailFolders?$top=100&$expand=childFolders($top=100)`, {
  method: "GET",
  headers: headers,
});

if (!response.ok) {
  throw new Error("GraphApi call failed to authenticate: " + response.statusText);
}

const data = await response.json();

处理超过100条的子文件夹

如果某个顶层文件夹下的子文件夹数量超过100,API会返回@odata.nextLink在对应的childFolders对象中。你需要循环请求这个链接来获取剩余的子文件夹,示例逻辑如下:

async function getAllChildFolders(topFolderId, accessToken) {
  let allChildFolders = [];
  let nextLink = `https://graph.microsoft.com/v1.0/me/mailFolders/${topFolderId}/childFolders?$top=100`;

  while (nextLink) {
    const response = await fetch(nextLink, {
      headers: { "Authorization": "Bearer " + accessToken }
    });
    if (!response.ok) throw new Error("Failed to fetch child folders");
    
    const data = await response.json();
    allChildFolders = [...allChildFolders, ...data.value];
    nextLink = data["@odata.nextLink"];
  }

  return allChildFolders;
}

你可以在获取顶层文件夹后,对每个文件夹调用这个函数来获取完整的子文件夹列表。

内容的提问来源于stack exchange,提问作者Ryan Durrant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 09:40:08