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

使用C# MS Graph上传文件到SharePoint时标识符格式错误问题

使用C#通过MS Graph上传文件到SharePoint的问题及解决方法

错误场景

  1. 直接使用站点完整域名路径报错
    尝试用以下代码定位站点并获取驱动器:
var scopes = new[] { "https://graph.microsoft.com/.default" };
var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

var authProvider = new ClientSecretCredential(_tenantId, _clientId, _clientSecret, options);
var client = new GraphServiceClient(authProvider, scopes);
var driveItem=await client.Sites["acme.sharepoint.com/sites/ndexport1"].Drive.GetAsync();

抛出错误:

Microsoft.Graph.Models.ODataErrors.ODataError: Provided identifier is malformed - id is not valid

  1. 仅指定域名无法定位目标子站点
    以下代码可正常执行,但只能定位到根站点,无法访问/sites/ndexport1/子站点:
client.Sites["acme.sharepoint.com"].Drive.GetAsync();
  1. 直接指定文档库路径报错
    尝试通过完整路径包含文档库来定位,同样报错:
var driveItem=await client.Sites["acme.sharepoint.com/sites/ndexport1/Documents/"].Drive.GetAsync();

错误信息:

Microsoft.Graph.Models.ODataErrors.ODataError: Resource not found for the segment 'Documents'.

可行解决方案

通过{域名}:/{站点相对路径}的格式正确定位站点,获取站点ID后再获取对应驱动器,最后完成文件上传:

var site=await client.Sites["acme.sharepoint.com:/sites/test2"].GetAsync();
var drive=(await client.Sites[site.Id].Drive.GetAsync());
var r= await client.Drives[drive.Id]
.Root.ItemWithPath("smallfile.txt")
.PutAsync(new MemoryStream(Encoding.ASCII.GetBytes("Hello, World")));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:46:04