使用C# MS Graph上传文件到SharePoint时标识符格式错误问题
错误场景
- 直接使用站点完整域名路径报错
尝试用以下代码定位站点并获取驱动器:
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
- 仅指定域名无法定位目标子站点
以下代码可正常执行,但只能定位到根站点,无法访问/sites/ndexport1/子站点:
client.Sites["acme.sharepoint.com"].Drive.GetAsync();
- 直接指定文档库路径报错
尝试通过完整路径包含文档库来定位,同样报错:
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
相关产品推荐
相关产品推荐

