Taxonomy Field Naming Convention in SharePoint

Background

In SharePoint 2013 when a full crawl happens, the Site Columns with data are managed and the crawled properties are created automatically. For a site column of type Taxonomy, SharePoint creates two types of crawled properties.

Consider my taxonomy field named MyTaxonomyField and there is a respective associated hidden Note field. In this case after the full crawl SharePoint will create two crawled properties as in the following:

ows_taxid_MyTaxonomyField and,
ows_MyTaxonomyField

The first crawled property is mapped with the automatically created managed property owstaxidMyTaxonomyField. This managed property we should use everywhere in our queries in a Content Search web part or in a Search Result web part since it contains the Term Id and we can query based on Term Ids.

For the second crawled property (ows_MyTaxonomyField), the managed property is not created automatically. We can create our own custom managed property and map with this crawled property. But then this is not the best practice, we couldn't query based on Term Ids since this property contains only Term Names and not the Ids. There might be the possibility of having the special characters in term names like “-“. Some of these special characters have a different meaning in Search (KQL) and that may cause the search queries failure or no result.

Problem

In our project, our Taxonomy fields first crawled properties (ows_taxid_MyTaxonomyField) are not being created and hence are not automatically created by the respective managed property. We have only a second crawled property (ows_MyTaxonomyField) created but we don't want to use it. In our Content Search web part we want to do the query based on TermId.

Solution

We have the following taxonomy field, here I kept only the important attributes for the field, the others are removed:

  1. <Field ID="{}"  
  2.          Type="Note"  
  3.          DisplayName="MyTaxonomyField _0"  
  4.          StaticName="MyTaxonomyField0"  
  5.          Name="MyTaxonomyField0"  
  6. </Field>  
  7.   
  8. <Field ID="{}"  
  9.          Name=" MyTaxonomyField "  
  10.          Type="TaxonomyFieldType"  
  11.          DisplayName=" MyTaxonomyField "  
  12.          StaticName="MyTaxonomyField ">  
  13. </Field>  
The first field is a hidden Note field and the second is a managed metadata field. Using the feature receiver we are associating the Note field with the Taxonomy field. 

After very much digging and Googling I found that the problem is in the naming convention of the Note field. The Note field associated with the managed metadata field should contain the “TaxHTField” in its static name or Name.

So the field should be like this:

  1. <Field ID="{}"  
  2.          Type="Note"  
  3.          DisplayName="MyTaxonomyField _0"  
  4.          StaticName="MyTaxonomyTaxHTField0"  
  5.          Name="MyTaxonomyTaxHTField0"  
  6. </Field>  
This is a strange behaviour but it works for our managed metadata fields. We spent a substantial amount of time finding this, hopefully if the same problem occurs then it will save you time.