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

Graph Items.GetAsync()要求Filter与文档矛盾,如何获取SharePoint文档库根项?

问题描述

我想要列出SharePoint文档库中的所有项,编写了如下代码:

var dokumenteId = "The Id of the document library";

var options = new TokenCredentialOptions
{
 AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var clientSecretCredential = new ClientSecretCredential(_tenantId, _clientId, _clientSecret, options);

var scopes = new[] { "https://graph.microsoft.com/.default" };

GraphClient = new GraphServiceClient(clientSecretCredential, scopes);

var drive = await GraphClient                
    .Drives[dokumenteId]
    .Items
    .GetAsync(); //this line throws the exception

执行代码时抛出异常:

The 'filter' query option must be provided.

查阅官方文档未提及需提供Filter参数,但文档示例让我困惑:

var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Children.GetAsync();

该示例要求传入driveItem-id参数,这在获取根目录时并不合理。请问如何获取站点下SharePoint文档库(即Drive)根目录中的文件夹和文件?

解决方案

直接调用Drives[dokumenteId].Items.GetAsync()的方式不正确,这个接口确实要求必须传入filter参数。获取文档库根目录的正确方式是访问根DriveItem的Children集合,SharePoint文档库的根目录对应的DriveItem ID固定为root,具体实现有两种方式:

方式一:直接访问根目录的Children

var rootItems = await GraphClient                
    .Drives[dokumenteId]
    .Items["root"]
    .Children
    .GetAsync();

方式二:通过站点路径访问文档库根目录(更直观)

如果知晓站点ID,也可以通过站点的lists路径关联文档库,再访问其根目录:

// 替换为实际站点ID
var siteId = "你的站点ID";
var listId = dokumenteId; // 文档库ID等同于List的ID

var rootItems = await GraphClient
    .Sites[siteId]
    .Lists[listId]
    .Drive
    .Root
    .Children
    .GetAsync();

如果需要递归获取文档库中所有层级的文件和文件夹,除了分页递归遍历,还可以使用Search接口查询所有项:

var allItems = await GraphClient
    .Drives[dokumenteId]
    .SearchWithQ("*")
    .GetAsync();

内容的提问来源于stack exchange,提问作者Mister 832

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 08:59:51