Published by Chris Johnson, on Saturday, March 22, 2014
This afternoon, I added a Content Query Web Part to a page and attempted to configure it to show all documents in the site collection where a Managed Metadata column equaled a certain value.
I was perplexed to see that the particular column wasn’t available in the filter options for the web part query– and furthermore, other Managed Metadata columns were available in the query filter. I immediately realized that the difference between this Managed Metadata column and the filterable Managed Metadata columns was that the column in question had the “Allow Multiple Values” checkbox selected in the column settings.
So, is it possible to use a Content Query Web Part to query on the value of a Multiple-Select Managed Metadata column? The answer (as is often the case in the SharePoint world) is “Sometimes.”
Using a Content Query Web Part to query on the value of a Multiple-Select Managed Metadata column is possible only if the CQWP is querying a specific list or library, meaning that the selected query source is “Show items from the following list.” Selecting the “Show items from all sites in this site collection” and “Show items from the following site and all subsites” query source will remove all Multiple-Select Managed Metadata fields from the query filter dropdowns.
Why? Because SharePoint constructs CAML queries using the WSSIDs of the term, which are unique to a site (and stored in the TaxonomyHiddenList). The WSSID is essentially a lookup column to the TaxonomyHiddenList on the site (url: /lists/TaxonomyHiddenList), which is populated as terms are tagged on items on the site; this enables SharePoint to quickly tag items and locate tagged items by only gathering data about the tags instantiated on a particular site, rather than indexing all terms regardless of whether or not they are used.
Because of this odd but purposeful architecture, a single term can have a different WSSID on each site on which it is used. This means that CAML queries are generated just as would be a CAML query for items with a Lookup column value. Microsoft’s documentation on Querying on Managed Metadata Columns confirms as such, even citing a sample CAML query using a Lookup value:
<Query><Where><In><FieldRef LookupId="TRUE" Name="ItemType" /><Values><Value Type="Integer">14</Value><Value Type="Integer">15</Value></Where></Query>
The LookupId is the WSSID of the term, which is obtained by calling the static “GetWssIdsOfKeywordTerm()” method of the Term class.
The complete resolution to this issue would require writing a web part that would (a) use server-side code-behind to call the GetWssIdsOfKeywordTerm() method for the user-supplied filter term on each site in the site collection, then (b) dynamically construct the CAML query that would individually query each site using the correct WSSID. Possible, but certainly not ideal. I may have to build this solution yet, so check back here for a follow-up post in the next couple of weeks.
Categories: SharePoint,SharePoint 2013,CAML query,content query web part,lookup list,managed metadata,multiple-select,multiple-select managed metadata,PSC,query filter,sharepoint,taxonomy,TaxonomyHiddenList,term store,web parts