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

使用C# Microsoft Graph SDK Expand参数查询SharePoint列表项时抛出异常

解决方案

问题出在Microsoft Graph C# SDK的QueryParameters.Expand属性会自动为参数名添加OData标准的$前缀,导致实际发送的请求参数是$expand=columns,而你测试发现该SharePoint列表项端点仅支持不带$的expand=columns参数,因此触发了OData错误。

修改代码,手动添加请求参数

绕过SDK自动添加$前缀的逻辑,直接通过QueryParameters.Add方法添加自定义参数:

var result = await graphClient.Sites["SITE_ID"].Lists["LIST_ID"].Items.GetAsync((requestConfiguration) => {
    // 手动添加不带$前缀的expand参数
    requestConfiguration.QueryParameters.Add("expand", "columns");
});

验证请求(可选)

如果需要确认实际发送的请求URL,或者排查其他潜在问题,可以捕获异常查看详细错误信息:

try
{
    var result = await graphClient.Sites["SITE_ID"].Lists["LIST_ID"].Items.GetAsync((requestConfiguration) => {
        requestConfiguration.QueryParameters.Add("expand", "columns");
    });
}
catch (ODataError odataError)
{
    // 输出详细错误信息用于排查
    Console.WriteLine($"错误代码: {odataError.Error.Code}");
    Console.WriteLine($"错误消息: {odataError.Error.Message}");
    Console.WriteLine($"内部错误: {odataError.Error.InnerError?.Message}");
}

这样修改后,请求URL会和你在Graph Explorer中测试的有效请求一致(?expand=columns),就能正常获取包含columns属性的列表项数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 12:30:01