无法使用客户端对象模型从 SharePoint 库中检索文件信息

本文关键字:检索 文件 信息 SharePoint 客户端 对象模型 | 更新日期: 2023-09-27 18:33:47

我正在尝试使用客户端对象模型从 SharePoint 2010 库中的文件读取某些属性。这是示例

using SP = Microsoft.SharePoint.Client;
SP.ClientContext clientContext = new SP.ClientContext( "http://path/to/the/site" );
clientContext.Load( clientContext.Web );
clientContext.ExecuteQuery();
SP.File spFile = clientContext.Web.GetFileByServerRelativeUrl("/TestLibrary/sample.pdf");
clientContext.Load(spFile);
clientContext.ExecuteQuery(); //here we'll catch exception

但我收到例外。

类型的第一次机会异常 "Microsoft.SharePoint.Client.ServerException" 发生在 Microsoft.SharePoint.Client.Runtime.dll

其他信息:值不在预期范围内。

我做错了什么?

无法使用客户端对象模型从 SharePoint 库中检索文件信息

由于 Web.GetFileByServerRelativeUrl 方法接受以下格式的参数serverRelativeUrl因此Value does not fall within the expected range.发生错误:

/Site_Name/SubSite_Name/Library_Name/File_Name

例:

using (var ctx = new ClientContext("https://intranet.contoso.com/news"))
{
    var file = ctx.Web.GetFileByServerRelativeUrl("/news/Documents/SharePoint User Guide.docx");
    ctx.Load(file);
    ctx.ExecuteQuery();  
}