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

SharePoint CSOM:文件复制操作出现数据类型不匹配异常

解决最新CSOM访问SharePoint Online文件复制成功却抛出InvalidOperationException的问题

问题现象

  • 使用最新版CSOM库访问SharePoint Online站点执行文件复制操作,操作实际已成功完成,但会抛出System.InvalidOperationException,错误提示:位置148处的数据类型与预期不符
  • 错误堆栈信息:
System.InvalidOperationException
  HResult=0x80131509
  Message=The type of data at position 148 is different than the one expected.
  Source=Microsoft.SharePoint.Client.Runtime
  StackTrace:
   at Microsoft.SharePoint.Client.JsonReader.ReadGuid()
   at Microsoft.SharePoint.Client.JsonReader.Read_Guid(JsonReader reader)
   at Microsoft.SharePoint.Client.JsonReader.Read[T]()
   at Microsoft.SharePoint.Client.ClientResult`1.Microsoft.SharePoint.Client.IFromJson.FromJson(JsonReader reader)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryToServerAsync>d__53.MoveNext()
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryAsync>d__39.MoveNext()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.<ExecuteQueryAsync>d__57.MoveNext()
   at Microsoft.SharePoint.Client.ClientContext.<ExecuteQueryAsync>d__23.MoveNext()
   at Program.<Main>d__5.MoveNext() in C:\Source\Playground\Program.cs:line 66
  • 服务器返回的响应体:
[
  {
    "SchemaVersion": "15.0.0.0",
    "LibraryVersion": "16.0.23710.12002",
    "ErrorInfo": null,
    "TraceCorrelationId": "1dc8b5a0-90d7-3000-a365-ef20f67bed77"
  },
  1,
  null
]
  • 补充:NuGet中无可用的15.0版本供降级

问题分析

从错误堆栈和响应体来看,问题核心是CSOM客户端的JSON解析逻辑与SharePoint Online服务端的响应格式不兼容:

  1. 服务器返回的响应第三个值为null,但CSOM的JsonReader.ReadGuid()方法预期此处是Guid类型数据,导致类型匹配失败抛出异常
  2. 文件复制操作已在服务端完成,说明异常仅来自客户端解析环节,不影响业务结果

解决方案

方案1:捕获特定异常并忽略(临时 workaround)

由于操作实际已成功,可在执行ExecuteQueryAsync()时针对性捕获该异常,不阻断后续流程:

try
{
    await clientContext.ExecuteQueryAsync();
}
catch (InvalidOperationException ex)
{
    // 仅忽略目标类型不匹配错误
    if (ex.Message.Contains("位置148处的数据类型与预期不符") || 
        ex.Message.Contains("The type of data at position 148 is different than the one expected"))
    {
        // 操作已完成,跳过异常
    }
    else
    {
        // 其他异常正常抛出
        throw;
    }
}

方案2:改用Microsoft Graph API执行文件复制

长期来看,迁移到Graph API可规避CSOM的版本兼容问题,示例代码如下:

// 初始化Graph客户端(需提前配置身份验证)
var graphClient = new GraphServiceClient(authProvider);

// 执行文件复制
await graphClient.Drives[sourceDriveId].Items[sourceItemId]
    .Copy(destinationFileName)
    .ToDrive(destinationDriveId)
    .ToPath(destinationFolderPath)
    .Request()
    .PostAsync();

方案3:等待CSOM官方修复

该问题属于客户端与服务端的响应格式适配bug,可关注NuGet上Microsoft.SharePointOnline.CSOM包的更新,待官方修复解析逻辑后升级对应版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:17:16