SharePoint 2010 Managed Metadata Import Ampersand

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!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.