简体   繁体   English

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

[英]SharePoint CAML Query - How to read MetaInfo field values programmatically?

I am using C# and SharePoint Client Object Model to query recursively across folders to get file properties in the collection returned. 我正在使用C#和SharePoint 客户端对象模型跨文件夹进行递归查询,以获取返回的集合中的文件属性。

I successfully can read the value from the ListItemCollection by specifying the field name like this: 我可以通过指定字段名称来成功地从ListItemCollection读取值:

listItem["Title"] 

but is there a way to read the individual values in 但是有一种方法可以读取其中的各个值

listItem["MetaInfo"]

which appears to have multiple custom meta data values, I suppose set by Microsoft office applications??? 它似乎具有多个自定义元数据值,我想是由Microsoft Office应用程序设置的??? Can I cast it to a type to get the individual values? 我可以将其转换为类型以获取单个值吗?

I do not want to be parsing the string.... 我不想解析字符串。

The data in the debugger for the MetaInfo feild looks like this: MetaInfo功能调试器中的数据如下所示:

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

Thank for your help and please excuse my ignorance - it is the first time I am dealing with SharePoint :) 感谢您的帮助,请原谅我的无知-这是我第一次使用SharePoint :)

SharePoint CSOM API does not contain builtin method for parsing MetaInfo field value which returns string representation of metadata information that is associated with the specified client object. SharePoint CSOM API不包含用于解析MetaInfo字段值的内置方法,该方法返回与指定客户端对象相关联的元数据信息的字符串表示形式。

You could consider the below method for parsing MetaInfo field value: 您可以考虑以下用于解析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]);
}

Usage 用法

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);
    //..

}

I have marked Vadim's solution as an answer however I thought I'll post my solution, which I did before I saw his response. 我已将Vadim的解决方案标记为答案,但是我认为我会发布解决方案,这是我在看到他的答复之前所做的。

The implementation below is using Extension methods and Regular Expression Match to extract the values - it is useful if you already know the fields you will be reading ie they are predefined - if you do not know what you'll be dealing with and they are dynamic then the answer above may be more useful to you. 下面的实现使用扩展方法和正则表达式匹配来提取值-如果您已经知道要读取的字段(即它们是预定义的),则很有用-如果您不知道将要处理的内容并且它们是动态的那么以上答案可能对您更有用。

Extension methods and class defining the meta info properties in the MetaInfo field you may need to retrieve: 您可能需要检索的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";
}

Usage 用法

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM