Monday, July 22, 2013

SharePoint 2010 - Two different Ampersand character symbols

It might be something trivial but it just ate a couple of hours of my time while debugging my SharePoint code. I thought to share it with others in case someone will be struggling with the same issue.
My code was failing to do a successful comparison between two similar looking strings.
I was trying to match the Taxonomy terms to find a matching term. The following value comparison might look equal but looking it closely you would say there is a bit of difference

Nut & Fruit = Nut & Fruit

The two ampersand characters are different, the one on the left was from the termset, while the one on the right from a single text field in a list. Anyways the resolution was simple, just update the termset with the standard character '&'

A quick resolution was to slightly modify my code to take care of this special scenario

 if (termVlaue.Contains("&") && term.Name.Contains("&"))
                    {
                        if (term.Name.Replace("&", "&").Equals(termVlaue))
                            return term;
                    }
                    else
                    {
                        if (term.Name.Equals(termVlaue))
                            return term;
                    }