简体   繁体   English

编写CAML查询以查询带有托管元数据列的SPList

[英]Compose a CAML Query to Query an SPList with Managed Metadata Column

I have an SPList with a managed metadata column and here is my caml query. 我有一个带有托管元数据列的SPList,这是我的caml查询。

/// This caml query doesn't work
SPQuery oQuery = new SPQuery();
string strQuery = @"<Query><Where><In><FieldRef LookupId=""TRUE"" Name=""TaxonomyColumn"" /><Values><Value Type=""Text"">7392ec1d-3f35-4c5b-b6ad-f80ff15ed718</Value></Values></In></Where></Query>";
oQuery.Query = strQuery;


/// This linq query works exactly fine
var itms = (from SPListItem itm in oList.Items
        where itm["TaxonomyColumn"].ToString().Contains("7392ec1d-3f35-4c5b-b6ad-f80ff15ed718")
        select itm).ToList();

My problem is I need to use CAML Query for some reason and I can't just figure out a way on how to compose my CAML Query to work on querying a list where the column is a managed metadata column. 我的问题是由于某种原因我需要使用CAML查询,而我不能仅仅想出一种方法来组成我的CAML查询,以便在查询列为托管元数据列的列表时工作。

Please help me. 请帮我。 Thanks. 谢谢。

SPQuery adds the Query tag itself: SPQuery添加了查询标签本身:

 SPQuery oQuery = new SPQuery();
string strQuery = @"<Query><Where><Contains><FieldRef Name='TaxonomyColumn' /><Value Type='Text'>7392ec1d-3f35-4c5b-b6ad-f80ff15ed718</Value></Contains></Where></Query>";
oQuery.Query = strQuery;

You don't use the GUID, you need to use the lookup the id. 您不使用GUID,需要使用查找ID。 You find this id using TaxonomyField.GetWssIdsOfKeywordTerm(), and write CAML like this: 您可以使用TaxonomyField.GetWssIdsOfKeywordTerm()找到此ID,并像这样编写CAML:

<Query>
  <Where>
    <In>
      <FieldRef LookupId="TRUE" Name="ItemType" />
      <Values>
        <Value Type="Integer">14</Value>
        <Value Type="Integer">15</Value>
      </Values>
    </In>
  </Where>
</Query>

"In" is a convenience when there are multiple values instead of multiple Eq/Or statements. 当有多个值而不是多个Eq / Or语句时,“ In”是一种便利。

Explained in detail here: http://msdn.microsoft.com/en-us/library/ff625182.aspx 此处详细说明: http : //msdn.microsoft.com/zh-cn/library/ff625182.aspx

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

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