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

ExecuteQuery()异常:输入字符串格式不正确,重复调用却正常?

问题分析与解决方案

问题描述

此前基于CSOM的PowerShell脚本操作SharePoint Online列表一直正常运行,但近期调用ExecuteQuery()时抛出**"输入字符串格式不正确"**的异常,偶然发现连续调用两次ExecuteQuery()时,第一次报错、第二次却能正常执行。

原脚本片段:

$spOcredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spOuser, $secpasswd)
$spOctx = New-Object Microsoft.SharePoint.Client.ClientContext($spOSiteURL)
$spOctx.Credentials = $spOcredentials

$spOList = $spOctx.Web.Lists.GetByTitle($spOListName)
$spOctx.Load($spOList)
$spOctx.Load($spOList.Fields)
$spOctx.ExecuteQuery()

报错信息:

Exception calling "ExecuteQuery" with "0" argument(s): "Input string was not in a correct format."
At C:\Develop\Kontrola sloupcu seznamu.ps1:36 char:1
+ $spOctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

可能原因

  1. 字段元数据解析冲突:一次性加载列表对象和字段集合时,部分字段(如计算字段、自定义格式字段)的元数据在服务端解析时出现格式异常,第一次请求触发错误后,服务端或客户端缓存了修正后的解析结果,第二次请求可正常执行。
  2. 列表内容格式异常:目标列表中存在不符合字段类型的内容(如数字字段存储了非数字字符串),第一次加载字段时触发格式校验错误,第二次请求跳过了部分校验逻辑。
  3. CSOM库或服务端临时bug:旧版本CSOM库存在已知的解析bug,或SharePoint Online服务端出现临时的解析异常,重试请求可绕过该问题。

解决办法

1. 拆分加载操作,分步执行ExecuteQuery()

将列表基本信息和字段集合的加载拆分为两次独立的ExecuteQuery()调用,避免一次性加载导致的解析冲突:

$spOcredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spOuser, $secpasswd)
$spOctx = New-Object Microsoft.SharePoint.Client.ClientContext($spOSiteURL)
$spOctx.Credentials = $spOcredentials

$spOList = $spOctx.Web.Lists.GetByTitle($spOListName)
# 先加载列表基本信息
$spOctx.Load($spOList)
$spOctx.ExecuteQuery()

# 再加载字段集合
$spOctx.Load($spOList.Fields)
$spOctx.ExecuteQuery()

2. 检查目标列表的字段与内容

  • 排查近期新增或修改的字段(尤其是计算字段、数字/日期类型字段),确认字段的默认值、计算公式是否存在格式错误。
  • 检查列表项内容,确认是否存在不符合字段类型的数据(如数字字段中包含字母、特殊字符)。

3. 增加带重试的异常捕获逻辑

如果是服务端临时异常导致的问题,可在脚本中增加重试逻辑,仅在第一次失败时执行重试:

$spOcredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spOuser, $secpasswd)
$spOctx = New-Object Microsoft.SharePoint.Client.ClientContext($spOSiteURL)
$spOctx.Credentials = $spOcredentials

$spOList = $spOctx.Web.Lists.GetByTitle($spOListName)
$spOctx.Load($spOList)
$spOctx.Load($spOList.Fields)

try {
    $spOctx.ExecuteQuery()
} catch {
    Write-Warning "首次执行请求失败,正在重试:$_"
    # 重置上下文后重试
    $spOctx = New-Object Microsoft.SharePoint.Client.ClientContext($spOSiteURL)
    $spOctx.Credentials = $spOcredentials
    $spOList = $spOctx.Web.Lists.GetByTitle($spOListName)
    $spOctx.Load($spOList)
    $spOctx.Load($spOList.Fields)
    $spOctx.ExecuteQuery()
}

4. 更新CSOM库版本

确保使用的SharePoint Online CSOM库是最新版本,旧版本可能存在已修复的解析bug。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 00:55:11