我正在使用C#和SharePoint 客户端对象模型来跨文件夹递归查询以获取返回的集合中的文件属性。
我成功地可以通过指定字段名称来读取ListItemCollection
中的值:
listItem["Title"]
但有没有办法阅读
中的各个值listItem["MetaInfo"]
它似乎有多个自定义元数据值,我想由Microsoft Office应用程序设置???我可以将其转换为类型以获取单个值吗?
我不想解析字符串......
MetaInfo
feild的调试器中的数据如下所示:
display_urn\\:schemas-microsoft-com\\:office\\:office#Editor:SW|System Account \nvti_parserversion:SR|14.0.0.7149 \nvti_folderitemcount:IR|0 ContentTypeId:SW|0x0101008B5F2095338FE647A7F89B5275681D66 vti_title:SW|Mr Foo Howe 26-03-2014 vti_author:SW|MYDOMAIN\\\\jblogs Document Type:SW|Contract Note vti_modifiedby:SR|SHAREPOINT\\\\system vti_foldersubfolderitemcount:IR|0 display_urn\\:schemas-microsoft-com\\:office\\:office#Author:SW|Blogs, Jo
感谢您的帮助,请原谅我的无知 - 这是我第一次与SharePoint打交道:)
答案 0 :(得分:2)
SharePoint CSOM API不包含 builtin 方法,用于解析MetaInfo
字段值,该值返回与指定客户端对象关联的元数据信息的字符串表示形式。
您可以考虑以下方法来解析MetaInfo
字段值:
/// <summary>
/// Parse MetaInfo field value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static Dictionary<string, string> ParseMetaInfo(string value)
{
return value.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(new[] {":"}, StringSplitOptions.RemoveEmptyEntries))
.ToDictionary(x => x[0], x => x[1]);
}
<强>用法强>
var list = ctx.Web.Lists.GetByTitle(targetListTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include( i => i["MetaInfo"]));
//ctx.Load(items);
ctx.ExecuteQuery();
foreach (var item in items)
{
var metaInfo = (string) item["MetaInfo"];
var metaInfoValue = ParseMetaInfo(metaInfo);
//..
}
答案 1 :(得分:0)
我已经将Vadim的解决方案标记为答案,但我想我会发布我的解决方案,这是在我看到他的回复之前做的。
下面的实现是使用扩展方法和正则表达式匹配来提取值 - 如果您已经知道要读取的字段,即它们是预定义的 - 如果您不知道将要处理的内容和它们是动态的,那么上面的答案对你来说可能更有用。
在您可能需要检索的MetaInfo
字段中定义元信息属性的扩展方法和类:
public static class Extensions
{
public static string[] DocMetaInfoFields =
{
MetaInfoFields.Title, MetaInfoFields.Author,
MetaInfoFields.DocumentType, MetaInfoFields.ModifiedBy
};
public static string GetDocumentType(this object metaInfo)
{
var match = GetRegexMatch(metaInfo as string, MetaInfoFields.DocumentType);
return (match.Groups.Count > 1)
? match.Groups[1].Value
: string.Empty;
}
public static Dictionary<string, string> GetDocMetaProperties(this object metaInfo)
{
var properties = new Dictionary<string, string>();
foreach (var field in DocMetaInfoFields)
{
var match = GetRegexMatch(metaInfo as string, field);
properties.Add(field,
(match.Groups.Count > 1)
? match.Groups[1].Value
: string.Empty);
}
return properties;
}
public static StringBuilder FormatCamlValues(this StringBuilder sb, string valueTag, string listName,
IEnumerable<string> clientReferences)
{
foreach (var clientRef in clientReferences)
{
sb.AppendFormat(valueTag, listName, clientRef);
}
return sb;
}
public static List<ClientDocumentListItem> ToClientDocumentList(this ListItemCollection files)
{
return files.ToList().ConvertAll(ListItemToClientDocItem);
}
private static Match GetRegexMatch(string searchString, string fieldName)
{
string regexCapture = string.Format(@"^{0}:\w{{2}}\|([^.(\r|\n)]*)[\r\n|\n\r]+\w", fieldName);
return Regex.Match(searchString, regexCapture, RegexOptions.Multiline);
}
}
/// <summary>
/// Defines the field names inside the MetaInfo composite field returned while using the SharePoint client object CamlQuery() method
/// </summary>
public static class MetaInfoFields
{
public static string MetaInfoFieldName = "MetaInfo";
public static string Title = "vti_title";
public static string Author = "vti_author";
public static string DocumentType = "Document Type";
public static string ModifiedBy = "vti_modifiedby";
}
<强>用法强>
var list = ctx.Web.Lists.GetByTitle(targetListTitle);
var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
ctx.Load(items, icol => icol.Include( i => i[MetaInfoFields.MetaInfoFieldName]));
//ctx.Load(items);
ctx.ExecuteQuery();
var docProperties = GetDocMetaProperties(listItem[MetaInfoFields.MetaInfoFieldName]);
var title = docProperties[MetaInfoFields.Title];
var createdBy = docProperties[MetaInfoFields.Author];
var modifiedBy = docProperties[MetaInfoFields.ModifiedBy];
var type = docProperties[MetaInfoFields.DocumentType];