SharePoint 2010 Taxonomy store and Ampersand issue

I ran into a weird issue while I was working with the SharePoint taxonomy term store to upload some managed metadata. I had modified the code to check for existing terms, and if they were not there to insert them. turns out there were no errors when I had tested it, but when I ran it against the production file I would get errors, that I was trying to insert a duplicate key. I did some tracing, and for some reason Ampersands in the term name were causing failures. further investigation showed that even though the term was in the comparison list, the compare criteria was failing. After a little more time, and some unsuccessful approaches, I notices that the ampersand in the watch window looked a little different from the comparison string vs the input string.

&& << look similar don’t they? SharePoint doesn’t think so

So I thought it was a different font, or bolded, or something. I tried pasting into excel and getting the unicode for the mystery character, and it did indeed come back as not an ampersand.. but what was it.. A little more searching and I cam e across this:

SharePoint 2010: Managed Metadata Converts Ampersand and Double Quotes to Unicode We have been making a lot of use of Managed Metadata lately. One use is to associate people with a team, so we have created a user profil…

that was the smoking gun. The system was transforming the input text as I was putting it in. so when I pulled it back out to compare to the new list, it was just subtly different enough to fail the comparison, and then once I tried to insert the text, it would transform it on the way in and throw an ugly error about key violations. Solution in my case was straight forward, I did not need to transform the text back into it original form. I just needed to know if what I was looking at was already in there. So I Used the TaxonomyItem.NormalizeName method to pre transform my input text and then compared that to what was already in the terms collection. worked out much better.


Originally posted on How SharePoint Annoyed Me Today.