I have good news for Sharepoint developers who use Camlex in their project: I’ve released new versions of Camlex and Camlex.Client which now support IN operation (finally). At first I will show examples and describe some technical details and then add several notes about using of Camlex in different Sharepoint versions (2007, 2010 and 2013).
What is IN operation? As msdn article says:
Specifies whether the value of a list item for the field specified by the FieldRef element is equal to one of the values specified by the Values element.
E.g. if we need to get all list items which have integer Count field in range 0..9 we can use the following query:
It was possible to combine multiple Eq operations using Or and get the same result. And with Camlex it was quite easy to do it using single line of code (for better formatting I put it to 3 lines):
But let’s see the resulting CAML of the above code:
Comparing with 1st example, it looks not very nice. If it doesn’t matter for you, you still can use OR syntax. However now it is possible to write simpler expression which will produce IN syntax:
It will produce CAML which we already saw:
For those who worked with NHibernate or Linq2Sql this syntax won’t be new: these ORM frameworks use the same syntax for generating of SQL queries with IN operator. I intentionally showed example with dynamically populated array (Enumerable.Range(0, 9)): code supports any expression which produces IEnumerable:
(Do you see the difference between the last example which function which returns List<int>, and previous example, which uses IEnumerable? If yes, you know C# well :). Class List<T> has own Contains method, while for IEnumerable<int> from first example Linq Contains extension method was used. As you can see Camlex may work with both of them. Note that if you use Linq you should add “using System.Linq;” to your file).
I.e. basic syntax is the following:
Of course you may write array with constants:
Also it will work with any type supported for Value element:
It will produce the following result:
Now it uses Text type in Value elements.
That was only half of the story. If you follow the Camlex, you probably know that starting with version 3.0 it became bidirectional (I wrote about it here: Camlex.NET became bidirectional and goes online. Version 3.0 is released). So all conversions are added with 2 directions: from expression to CAML and from CAML to expression:
Applications of that feature:
1. on http://camlex-online.org/ add xml shown below to the textarea and click Convert to C#. It will show you how this query will be produced by Camlex:
2. it is possible to add more conditions to the existing IN query which is stored as xml, or add IN query to existing query:
I.e. Camlex may combine several string queries using Or and And, or even mix string query with expression as shown above. This feature was added in previous releases.
That was part with examples. Now about using of Camlex in different Sharepoint versions. Camlex was initially developed for Sharepoint 2007 and it references Microsoft.SharePoint.dll of 184.108.40.206 version (it only references it for compilation. Camlex doesn’t call any methods of Sharepoint API). However thanks to assemblies binding redirect in Sharepoint it works also in 2010 and 2013 (see this post for details why: Assembly binding redirect in Sharepoint 2010: how old code for SP 2007 works in SP 2010). IN CAML operation was introduced with Sharepoint 2010, i.e. it won’t work in Sharepoint 2007. So before to release new version 3.5 with IN operation support I choose between 2 options:
1. add it to existing Camlex and continue to use Microsoft.SharePoint.dll of 220.127.116.11 version.
Pros: one dll which works in all Sharepoint versions, it is simpler for developers who will want to install it.
Cons: IN element will be created now also for Sharepoint 2007, but it won’t work when try to execute this query against data.
2. create separate branch for Sharepoint 2007 and leave reference to 18.104.22.168 assembly only there, while in default branch change it to 22.214.171.124, so it will be used only starting with Sharepoint 2010, where IN operaion is supported.
Pros: each branch will have only supported features in appropriate version.
Cons: developers should choose correct version by themselves, codeplex is not very good in showing them, it is quite simple to get lost in releases there.
Also with 2nd option it would be necessary to maintain one more branch (currently there are 2 branches: default for basic Camlex and client branch for Camlex for client object model. BTW Camlex.Client.dll of version 1.3 supports IN operator as well). As currently I’m the only continuous contributor of the project and I have very limited time, I choose option 1 for now. May be in future I will separate branches, but for a while there will be single Camlex branch which will work with all Sharepoint versions, but generated CAML may not work in Sharepoint 2007.
With codeplex assemblies I also updated nuget packages. You can install latest versions by the following commands:
for client object model:
In my next plans to add rest of CAML elements introduced with Sharepoint 2010 (Include, NotInclude) and Joins. They also should be added of course at some point of course.