We want to filter content on managed terms. Create a new column with metadata field type and bind it to a termset from the termstore. Create new content types with the metadata columns. Add some content based on it (lists, calender, pages) with different metadata. To retrieve data you for example use contentbyquerwebpart or Spquery. We want to set the query to a caml query filtered on a metadata field. A metadata field is a lookup type, and you can not write a query as it was a text field (althoug the term itself is text).

<code> &lt;Eq&gt;&lt;FieldRef ID='" + metacolumnid + "' LookupId='TRUE' Nullable='TRUE'/&gt; &lt;Value Type='Lookup'&gt;" +id + "&lt;/Value&gt;&lt;/Eq&gt; </code>

The Fieldref ID is the GUID of the metadata column. The lookup value is the GUID of the term we want to filter on. Or it is not the GUID itself, but a lookup property: WssId.  This is an identifier (ID) of the list item that the lookup field points to. And how to find it? Well it took me some time to find out.. There is some hidden magic behind the scenes! All terms are actually stored in a list (everything in Sharepoint is lists, so not a big suprise :). This list is hidden, but can be found: [site url]/Lists/TaxonomyHiddenList/AllItems.aspx.

Based on the IdForTerm we can retreive the wssid by TaxonomyField.GetWssIdsOfTerm

<code> TaxonomyField.GetWssIdsOfTerm(site, _ termStoreId, termSetId, termId, includeDescendants, _ limit) </code>

But we don’t want to hardcode the id values. To get the data more dynamically, we can create term store objects based on the default termstore and we probably know which group and termset we filter on (because a metadata column is bound to a termset).

<code> TaxonomySession session = new TaxonomySession(curSite); TermStore termStore = session.DefaultSiteCollectionTermStore; Group termGroup = termStore.Groups["MyGroup"]; TermSet termSet = termGroup.TermSets["MyTermset"]; String metacolumnid = "{603E67C1-99C9-4EB7-B4D8-A299DF4AF468}"; //metadata column id ............ private int GetTermWssId(SPWeb rootWeb, string termtitle, TermStore termStore, TermSet termSet) { Guid guidid = new Guid(); int result = 0; if (rootWeb.Properties.ContainsKey("TaxonomyHiddenList")) { Guid taxonomyHiddenListId = new Guid(rootWeb.Properties["TaxonomyHiddenList"]); SPList taxonomyHiddenList = rootWeb.Lists[taxonomyHiddenListId]; SPQuery query = new SPQuery(); // we might have included the IdForTermSet in the query but we assume // that Guid is really unique // so there should not be temrs in other terms sets having the same ID string guid = termSet.Id.ToString(); guid = guid.Replace("{", ""); guid = guid.Replace("}", ""); query.Query = String.Format(@"{0}{1}", termtitle, guid); // query.Query = String.Format(@"{0}", guid); SPListItemCollection items = taxonomyHiddenList.GetItems(query); if (items.Count == 1) { guidid = new Guid(items[0]["IdForTerm"].ToString()); int[] wssIds = TaxonomyField.GetWssIdsOfTerm(rootWeb.Site, termStore.Id, termSet.Id, guidid, false, 1); result = wssIds[0]; } } return result; } </code>

This is the solution I came up with, maybe it could be solved more elgant than this?

Another thing to remember.. To be able to query the metadata store and to get the above code to work, make sure the conent type syndication is activated on your site.