Saturday, March 30, 2013

Camlex.Net 3.4 and Camlex.Client 1.2 are released

Today I’ve released new versions of the Camlex.Net open source project: Camlex.Net 3.4 (basic Camlex for Sharepoint object model) and Camlex.Client 1.2 (Camlex for client object model). Both libraries are also available in NuGet gallery. In order to add them to your project execute the following commands in VS Package manager:

Install-Package Camlex.NET.dll

Install-Package Camlex.Client.dll

Interesting that it was the 1st release which contains pull request from another user’s fork. In both branches the following new features were added:

1. Join several string conditions into single query

Now it is possible to pass array of string conditions for CAML query. Camlex will join them and will build correct CAML tree. This feature will be useful if you have some other tools or libraries which work with string queries, and would like to simplify joining of these conditions. For example if we have 3 following conditions:

   1: var expr = new List<string>();
   2: expr.Add(
   3:     "<Where>" +
   4:     "   <Eq>" +
   5:     "       <FieldRef Name=\"Field1\" />" +
   6:     "       <Value Type=\"Integer\">1</Value>" +
   7:     "   </Eq>" +
   8:     "</Where>");
   9: expr.Add(
  10:     "<Where>" +
  11:     "   <Eq>" +
  12:     "       <FieldRef Name=\"Field2\" />" +
  13:     "       <Value Type=\"Integer\">2</Value>" +
  14:     "   </Eq>" +
  15:     "</Where>");
  16: expr.Add(
  17:     "<Where>" +
  18:     "   <Eq>" +
  19:     "       <FieldRef Name=\"Field3\" />" +
  20:     "       <Value Type=\"Integer\">3</Value>" +
  21:     "   </Eq>" +
  22:     "</Where>");

and want to create common query which will return all items which have all conditions met, i.e. Field1 = 1 AND Field2 = 2 AND Field3 = 3, then we can write:

   1: string caml = Camlex.Query().WhereAll(expr).ToString();

and it will produce the common query:

   1: <Where>
   2:   <And>
   3:     <And>
   4:       <Eq>
   5:         <FieldRef Name="Field1" />
   6:         <Value Type="Integer">1</Value>
   7:       </Eq>
   8:       <Eq>
   9:         <FieldRef Name="Field2" />
  10:         <Value Type="Integer">2</Value>
  11:       </Eq>
  12:     </And>
  13:     <Eq>
  14:       <FieldRef Name="Field3" />
  15:       <Value Type="Integer">3</Value>
  16:     </Eq>
  17:   </And>
  18: </Where>

As you can see Camlex created correct result conditions tree automatically. By the same way you may create query which will return items which have at least one condition met, using WhereAny method (it will use Or instead of And).

2. Support for native double type

Prior to 3.4 version (and 1.2 client version) in order to create queries with Number data type, you needed to use string-based syntax:

   1: string caml = Camlex.Query().Where(x => x["Foo"] ==
   2:     (DataTypes.Number)"1.2").ToString();

which will produce the following CAML:

   1: <Where>
   2:   <Eq>
   3:     <FieldRef Name="Foo" />
   4:     <Value Type="Number">1.2</Value>
   5:   </Eq>
   6: </Where>

It is called string-based, because rvalue is represented by string, which is casted to the CAML datatype (DataTypes.Number in the example above). We introduced string-based syntax, because there is no 1 to 1 mapping between C# types and CAML types (e.g. User or WorkflowStatus CAML types). But there are several types which exist both in CAML and in C#: int and Integer, string and Text, DateTime and DateTime. Now double C# type is mapped to Number CAML type. So it is possible to rewrite the above query using shorter version:

   1: string caml = Camlex.Query().Where(x => (double)x["Foo"] == 1.2).ToString();

As you can see now rvalue is represented by real C# double which is more simpler and intuitive.

This change is also first change which was added from fork of another user (not Camlex developer) with nickname Shapel who sent pull request to Camlex using Codeplex functionality. So I would like to thank Shapel for this contribution and remind that Camlex is free open source project which is open for your contribution. So please fell free to create your forks and send pull requests. It will help to make Camlex more valuable for our every day work.

No comments:

Post a Comment