【发布时间】:2014-12-31 21:27:16
【问题描述】:
给出的信息
我正在使用 Microsoft.SharePoint.Client 库的 16.0.0.0 版。 SharePoint 网站中有几个不同的文档库。在代码中,我们得到一个文档 URL 列表(包含文档 GUID),需要确定每个文档位于哪个文档库中,如果它位于库中的文件夹中,则包括子文件夹。
文档 ID (GUID) - 从文档 url 查询参数“sourcedoc”收集 例如:“http://mycompany.sharepoint.com/spsite/_layouts/15/WopiFrame.aspx?action=default&file=testfile.docx&sourcedoc={6A290A65-4759-41ED-A13E-3333B45DF133}”
需要的信息
- 文档库名称
- 文档库 URL
- 文件夹名称(如果有)
- 文件夹 URL(如果有)
当前代码
using SP = Microsoft.SharePoint.Client;
public class LibraryAndFolderInfo
{
public LibraryAndFolderInfo();
public string FolderName { get; set; }
public string FolderUrl { get; set; }
public string LibraryName { get; set; }
public string LibraryBaseUrl { get; set; }
}
public class SPDataAccess
{
public LibraryAndFolderInfo GetLibraryAndFolderInfo(Guid documentGuid)
{
SP.File file = Web.GetFileById(documentGuid);
Context.Load(file);
if (file != null)
{
Context.Load(file.ListItemAllFields);
Context.ExecuteQuery();
SP.ListItem item = file.ListItemAllFields;
Context.Load(item.ParentList);
Context.ExecuteQuery();
SP.List list = item.ParentList;
Context.Load(list);
Context.ExecuteQuery();
Context.Load(item.Folder);
Context.ExecuteQuery();
SP.Folder folder = item.Folder;
Context.Load(folder);
Context.ExecuteQuery();
LibraryAndFolderInfo lib = new LibraryAndFolderInfo();
lib.LibraryName = list.Title;
lib.LibraryBaseUrl = list.DefaultViewUrl;
lib.FolderName = folder.Name;
lib.FolderUrl = folder.ServerRelativeUrl;
return lib;
}
return null;
}
protected SP.ClientContext Context { get; set; }
}
代码当前在这一行失败:Context.Load(item.ParentList); 并出现以下错误:
“标题”属性或字段尚未初始化。它尚未被请求或请求尚未执行。可能需要明确请求。
在 Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(String propName)
在 Microsoft.SharePoint.Client.List.get_Title()
在 c:\path\SPDataAccess.cs:line 27 中的 MyNamespace.SPDataAccess.GetLibraryAndFolderInfo(Guid documentGuid)
此行似乎正在尝试检索列表的标题,因为它正在加载并且失败。这是一个正确的假设吗?
我对如何准确地加载属性并不是非常熟悉,所以失败行之后的所有内容都是我对它如何工作的最佳猜测。
这段代码应该是什么样子的?有其他人尝试从文档中获取此信息吗?
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
感谢标题编辑。我刚开始提问。
-
好的,请看How to Ask。
标签: c# sharepoint sharepoint-2013