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

使用Graph API更新SharePoint项时提示‘Item not found’错误

使用Graph API更新DriveItem时提示“项未找到”的问题

我在使用Graph API遍历项列表,尝试更新对应项时一直收到“Item not found”(项未找到)的响应。权限设置显示我拥有该项的查看权限,且已经匹配了项名称,理论上应该能正常执行操作。

相关代码如下:

var drives = await graphClient.Drives.GetAsync();
var rootDrive = drives.Value[0];
var items = await graphClient.Drives[rootDrive.Id].List.Items.GetAsync();
foreach (var item in items.Value)
{
    if (item.Name == fileName || item.WebUrl.Contains(fileName))
    {
        var requestBody = new DriveItem
        {
            Content = fileBytes, 
        };
        var result = await graphClient.Drives[rootDrive.Id].Items[item.Id].PatchAsync(requestBody); // 返回itemnotfound
    }
}

附带截图显示:请求返回404状态码,响应信息明确为“Item not found”。

核心问题排查及解决办法

  • 权限不足:查看权限不等于编辑权限,你得确认是否拥有Files.ReadWrite、Sites.ReadWrite.All这类可修改的权限,只读权限无法执行更新操作。
  • ID使用错误:List.Items返回的是列表项ID,并非DriveItem的ID!你拿着列表项ID调用Drive的API,系统自然找不到对应文件。正确做法是从列表项中获取关联的DriveItem:
    // 先获取列表项对应的DriveItem
    var driveItem = await graphClient.Drives[rootDrive.Id].List.Items[item.Id].DriveItem.GetAsync();
    // 再用DriveItem的ID更新内容
    await graphClient.Drives[rootDrive.Id].Items[driveItem.Id].Content.PutAsync(fileBytes);
    
  • 更新方式错误:PatchAsync用于更新文件元数据(如名称、描述),更新文件内容应该用Content.PutAsync,直接传入字节数组即可,无需构造DriveItem对象。
  • Drive定位错误:drives.Value[0]不一定是你要操作的文档库对应的Drive,建议通过站点ID或文档库名称精准定位,避免选错目标。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 11:48:16