In this article I would like to show one of the practical usage of free and open source Camlex.NET library in real life scenarios of Sharepoint development. In various Sharepoint forums I often see questions where people ask how they achieve mentioned goal, i.e. how they can build dynamic CAML queries based on set of parameters specified e.g. in query strings. In most cases I answered by giving a link to Camlex.NET project on codeplex with some brief example. But with this I would like to have more descriptive blog post which will help people in their every-day work. This is one of the purpose of this post. Another purpose is to show how easy such tasks can be achieved with Camlex.NET – because this library was created exactly for simplifying dynamic CAML queries building (see also my previous articles which cover dynamic CAML queries: http://sadomovalex.blogspot.com/2010/02/build-dynamic-expressions-for-caml.html (this article for Camlex.NET 1.0. One the moment of writing this post version 2.0 is current and recommended release which solves mentioned problems more simpler) and http://sadomovalex.blogspot.com/2010/04/camlexnet-20-for-sharepoint-released.html).
So lets start from the more or less common scenario: there is number of fields and we want to be able to retrieve items from Sharepoint list based on values specified in query string, e.g. if we will enter the following URL http://example.com/_layouts/Custom/Search.aspx?Title=Meeting we want to see only those items which have Title field equal to “Meeting”. Assume that all query string parameters are optional – i.e. if some parameter is not specified in query string search engine should ignore it. Also if several query string parameters are specified we will treat them as single boolean expression joined using logical And (&&), i.e. http://example.com/_layouts/Custom/Search.aspx?Title=Meeting&Description=Sharepoint means that we want to retrieve item which have Title=Meeting && Description=Sharepoint. This is quite common scenario where we applied useful pattern when dealing with web search: search is performed using HTTP GET request so users will be able to bookmark search result in their browsers (that’s how most popular search engines works also). Note that in real life you probably want to join conditions using logical Or (||) – it is not problem also.
At first download Camlex.NET assembly from http://camlex.codeplex.com, and install Camlex.NET.dll into GAC. Now you are ready to use Camlex.NET library in your project. For test purposes I created simple Sharepoint list “TestList” based on Custom list template and added one single line text field Description in addition to existing Title field. And then I added several test items in it. In order to test results I created new application _layouts page Search.aspx in 12/template/layouts/custom folder with included server code (application _layouts pages are good tool for testing because they are compiled on the fly and don’t require reinstall assemblies in GAC with app pool recycling). I will provide full code of Search.aspx page, but the real work is only 2 lines of code – the rest is just infrastructure stuff:
Note the following lines of code:
All work is done in these lines: at first we construct list of expressions based on query string parameters (i.e. we build expression tree representation for each key value pair in query string) and then we call WhereAll() method of Camlex.NET library which joins expressions using logical And (&&). So when we specify URL http://example.com/_layouts/Custom/Search.aspx?Title=Meeting&Description=Sharepoint Camlex.NET will build the following CAML query:
It is scallable code, i.e. if we will add new field (Foo) and append it to URL: http://example.com/_layouts/Custom/Search.aspx?Title=Meeting&Description=Sharepoint&Foo=bar then Camlex.NET will automatically build valid CAML query with this new condition:
That’s how building of dynamic CAML queries can be achieved with Camlex.NET library. In the future posts I will show how to use another data types (i.e. not just strings) and various operations (like >, <, Contains and other).