In the last post we addressed the basics of implementing the SharePoint thesaurus file using a SharePoint list. This included creating the content type, fields and list schema we would use to make this a reality. As part of that exercise we also created a custom action, a ribbon button, that we could use to generate the thesaurus from the list item entries. This post will address the page that the custom action links to and the code behind that does all the work.
As part of creating the custom action we linked to a page in the _layouts/blog folder called thesaurus.aspx and passed that a query string parameter called List, which was the ListId for the list where the custom action was clicked. Using that list identifier we want to iterate each of the entries that exist and create a corresponding entry for our thesaurus file. The syntax for the thesaurus file is pretty simple and shown below.
The code to generate the thesaurus is also fairly straight forward. The more difficult part of generating this file is that it must be in Unicode. In our case UTF-16 LE (little-endian), which means that it must have a byte-order mark (BOM). The image below is a partial hex representation of the file above. The hex values are shown in the middle and the output on the right. Notice the first two hex characters, FF and FE and the glyphs that those hex values represent. I have found that if your thesaurus file doesn’t contain these BOMs SharePoint has trouble parsing it. Most people just edit the existing thesaurus file, so they don’t run into this. You can’t see them in the file, but they are there. The trick to generate your own from scratch and have it work is to output these characters to begin the file and also, because it is Unicode, have the double bytes produced, shown below as the periods between the characters. We won’t go into Unicode or double byte characters as part of this exercise. There is plenty of information out there for those interested in learning more.
Back to our task at hand. It seems simple enough, and it should be, although I went through several iterations before I could get it just right. The key, at least that I found, is the stream that is used to output the file. The code is shown below.
In all the attempts I made to get the output correct I used an XmlWriter to create the thesaurus file. I tried a MemoryStream, StringBuilder and a few others for backing the XmlWriter with an actual stream but had no luck. Although we set the XmlWriterSettings to use Unicode and in the other instances I set the stream to do the same, it seems (again in my limited testing) they don’t output this as I would expect, which may be a limited understanding on my part. It wasn’t until I used a StreamWriter, with a MemoryStream together that I was able to get the output correct. The rest is pretty straight-forward, iterate the list items in the list and output to the file based on the type of thesaurus entry being made.
Once deployed the custom action should look similar to the screenshot below. By clicking the ‘Create Thesaurus File’ button the file will be generated and you are prompted to save it locally.
There are several enhancements that could be made to the project. These include the ability to validate the data in the list. The SharePoint thesaurus doesn’t like dashes (-) in values or duplicate entries. It could also be enhanced to generate thesaurus files for different countries based on a column that indicates the target country. I am sure there are other improvements that could be made as well.
I placed the completed solution on MSDN Code Gallery. You can download the completed project here.