Unable to retrieve file information from SharePoint library using Client Object Model

放肆的年华 提交于 2019-12-24 04:56:35

问题


I am trying to read some attributes from a file in SharePoint 2010 library using client object model. Here is the sample

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

But I am recieving exception.

A first chance exception of type 'Microsoft.SharePoint.Client.ServerException' occurred in Microsoft.SharePoint.Client.Runtime.dll

Additional information: Value does not fall within the expected range.

What I am doing wrong?


回答1:


The error Value does not fall within the expected range. occurs since Web.GetFileByServerRelativeUrl method accepts serverRelativeUrl parameter in the following format:

/Site_Name/SubSite_Name/Library_Name/File_Name

Example:

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();  
}


来源:https://stackoverflow.com/questions/27404570/unable-to-retrieve-file-information-from-sharepoint-library-using-client-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!