如何以编程方式读取MetaInfo字段值?

我正在使用C#和SharePoint 客户端对象模型在文件夹中递归查询以获取返回的集合中的文件属性。

通过指定字段名称,我可以成功读取ListItemCollection的值:

listItem["Title"] 

但有没有一种方法可以读取单个值

listItem["MetaInfo"]

这似乎有多个自定义元数据值,我想由微软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 :)


SharePoint CSOM API不包含解析MetaInfo字段值的内置方法, 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);
    //..

}

我已经将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)]*)[rn|nr]+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];
链接地址: http://www.djcxy.com/p/64151.html

上一篇: How to read MetaInfo field values programmatically?

下一篇: SharePoint 2013 CAML query OrderBy not sorting properly