SharePoint and Ampersands

SharePoint | Tags: ,

I work with Microsoft SharePoint and have come to know it as a system designed for end users first and developers can ‘work it out!’ That is probably a harsh criticism, I’m sure all developers get annoyed at certain elements of the technology they work with. This is one particular thing that came up and drove me insane until I discovered what was happening.

I was writing a CAML query that included passing in the Title of the page as a parameter, but kept getting no results despite being sure that the query was correct. I could see the fields in SharePoint that should return, but alas the empty set kept coming back. After too long searching I was able to finally find the truth. When adding a Term to the Term Store, if it includes an ampersand (&), SharePoint (in its infinite wisdom) decides to convert that to a wide ampersand (&).

Given my CAML query was taking in a parameter dynamically that included an ampersand on some occasions, I needed to replace these whenever they arose. I had to write a little regex to alter my CAML query before it was executed to substitute in wide ampersands for any regular ampersands. This was essentially:

Regex rgx = new Regex("&");
string text = "My CAML query";
text = rgx.Replace(text, "\uff06");

I find it incredible that the approach taken is to adjust the ampersand in Term Store entries, but not elsewhere on SharePoint (basically begging for this issue to arise), so be aware of the devilish ways of Microsoft and in particular SharePoint!