I have recently been developing a solution which imports key data into a SharePoint list and tags it using values (i.e. managed metadata) set in the Term Store. These tags are stored in a number of Managed Metadata Columns (aka TaxonomyFields) that are hooked up to specific Term Sets. I showed in a recent post that setting these values was fairly straightforward – however, I have come across something that frustrated me for a couple of hours. I had everything working as I wanted and then suddenly when deploying to another environment it started failing for certain terms. I then noticed that it was failing consistently on those terms that contained an ampersand. So I searched and found an important piece of uinformation that I was unaware of – the & character is replaced internally with the wide character version of the character (\uFF06). What? So a simple call to the Replace method on the string you are searching for should solve it.
labelToSearchFor = labelToSearchFor.Replace(/&/g,”\uFF06”);
Even better is that Microsoft have provided a static method to help with this on the TaxonomyItem class, which makes the call to Replace unnecessary.
In fact the MSDN documentation for this method remarks, “The name will be normalized to trim consecutive spaces into one and replace the & character with the wide character version of the character (\uFF06).“. Jobs a good ‘un.
Big thanks to David Winchurch and Ilya Lehrman for showing me the road….
MSDN – TaxonomyItem.NormalizeName Method