I’ve been working on synchronizing a Taxonomy stored in a separate database with SharePoint 2010′s Managed Metadata Service. A fundamental part of this is is the ability to detect existing terms in the term store and make sure that they aren’t duplicated. I tried doing this by string matching on the names as you would expect.
It worked fine until I got to terms with an ampersand (&) character in the name. The string comparison claimed that two seemingly identical strings were different but then complained when I tried to add the “different” term.
It turns out that SharePoint replaces the & character with it’s Unicode equivalent, a process it calls normalization, which means that I was trying to compare the ASCII & with the Unicode version hence the failed comparison.
Fortunately there’s a helper method, TaxonomyItem.NormalizeName, which will convert the comparison string to the correctly normalized variant. So:
var termSetQuery = termSet.Terms.Where(t => t.Name == vocabularyTerm.Name);
Becomes:
var termSetQuery = termSet.Terms.Where(t => t.Name == TermSet.NormalizeName(vocabularyTerm.Name));
Problem Solved!