Sharepoint客户端对象模型:使用包含的File.ServerRelativeUrl从列表中加载项目

时间:2012-01-30 05:12:18

标签: sharepoint include client

我在从列表中加载项目时尝试加载File.ServerRelativeUrl,但没有运气。

尝试此代码:

public IEnumerable<ListItem> EnumListItems(List list, Folder folder)
{
    if (folder == null)
        return null;

    var camlQuery = new CamlQuery
    {
        //  ViewXml = "<View Scope='RecursiveAll'></View>",
        FolderServerRelativeUrl = folder.ServerRelativeUrl//"Relative path to folder '); -> Add relative Path for your Folder. for e.g.  "/Lists/ListName/FolderName"
    };

    var collListItem = list.GetItems(camlQuery);

    var result = Context.LoadQuery
    (
        collListItem.Include
        (
            item => item,
            item => item.File,
            item => item.File.ServerRelativeUrl
            //item => item["Body"]
        )
    );

    Context.ExecuteQuery();
    return result;
}

但是当我收到ServerRelativeUrl时,没有运气:

  

'item.File.ServerRelativeUrl'抛出了类型'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'的异常

2 个答案:

答案 0 :(得分:2)

希望它可以帮助某人。在迭代文档库的列表项时,您可以使用以下示例代码获取文件的服务器相对URL:

var fileRef = Convert.ToString(listItem["FileRef"]);

答案 1 :(得分:0)

答案是 - 列表不是文档列表,然后文件始终为null且未初始化。

这里有一个示例获取项目attachmetns的相对url(以及我最终需要的内容)

string.Format(
  @"{0}/Attachments/{1}",
  list.RootFolder.ServerRelativeUrl,
  item.Id
)

由此加载的list.RootFolder.ServerRelativeUrl

var result = Context.LoadQuery
        (
            collList.Include
            (
                list => list.Title,
                list => list.Id,                    
                list => list.RootFolder,
                list => list.RootFolder.ServerRelativeUrl,
                list => list.Fields
            )
        );