SharePoint CAML查询-如何以编程方式读取MetaInfo字段值



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

我可以通过指定如下字段名成功地从ListItemCollection读取值:

listItem["Title"] 

但是有没有办法读取中的各个值

listItem["MetaInfo"]

它似乎有多个自定义元数据值,我想是由Microsoft office应用程序设置的???我可以将其强制转换为一个类型以获得单个值吗?

我不想解析字符串。。。。

调试器中MetaInfo字段的数据如下所示:

display_urn\\:schemas microsoft.com\\:office\\:office#编辑器:SW|系统帐户\nvti_parserversion:SR | 14.0.0.7149\nvti_foldermitemcount:IR|0ContentTypeId:SW|0x0101008B5F2095338FE647A7F89B5275681D66vti_title:SW|Foo Howe先生2014年3月26日vti_author:SW|MYDOMAIN\\\\jblogs文件类型:SW|合同注释vti_modified:SR|SHAREPOINT\\\\systemvti_foldersubfolderitemount:IR|0display_urn\\:schemas microsoft.com\\:office\\:office#作者:SW|Blogs,Jo

感谢您的帮助,请原谅我的无知-这是我第一次处理SharePoint:)

SharePoint CSOM API不包含用于分析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的解决方案标记为答案,但我想我会发布我的解决方案,我在看到他的回复之前就发布了。

下面的实现使用Extension方法和Regular Expression Match来提取值-如果您已经知道将要读取的字段(即它们是预定义的),这很有用-如果您不知道将要处理什么,并且它们是动态的,那么上面的答案可能对您更有用。

在您可能需要检索的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];

相关内容

  • 没有找到相关文章

最新更新