SharePoint CAML Helper Update

A couple of years ago I wrote a CAML helper project and uploaded the source code to CodePlex.

Recently I updated the code with a few small bug fixes, after some feedback from a colleague who used it successfully on a large scale Portal project.

As a result, I thought I’d write a short post to let those who weren’t aware of the code that it was there.

To view the C# source for the tool, or download the latest assembly, please follow this link.

To create CAML queries with the assembly, work with the CAMLManager class to build your query.

CAMLManager mgr = new CAMLManager();
 
mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "A Title"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "Another Title"));
 
string caml = mgr.GetCAML();

The GetCAML method will return the following string:

<Where>
  <Or>
    <Eq>
      <FieldRef Name="Title" />
      <Value Type="Text">A Title</Value>
    </Eq>
    <Eq>
      <FieldRef Name="Title" />
      <Value Type="Text">Another Title</Value>
    </Eq>
  </Or>
</Where>


Another example, with a more nested query could be:

CAMLManager mgr = new CAMLManager();
 
mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "A Title"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "Another Title"));
 
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "1"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Eq, "2"));
 
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.BottomGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "3"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.BottomGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Eq, "4"));
 
string caml = mgr.GetCAML();

This time, the result returned from the GetCAML method is:

<Where>
  <And>
    <Or>
      <Eq>
        <FieldRef Name="Title" />
        <Value Type="Text">A Title</Value>
      </Eq>
      <Eq>
        <FieldRef Name="Title" />
        <Value Type="Text">Another Title</Value>
      </Eq>
    </Or>
    <And>
      <Or>
        <Or>
          <Geq>
            <FieldRef Name="Total" />
            <Value Type="Number">1</Value>
          </Geq>
          <Eq>
            <FieldRef Name="Total" />
            <Value Type="Number">2</Value>
          </Eq>
        </Or>
        <Geq>
          <FieldRef Name="Total" />
          <Value Type="Number">3</Value>
        </Geq>
      </Or>
      <Eq>
        <FieldRef Name="Total" />
        <Value Type="Number">4</Value>
      </Eq>
    </And>
  </And>
</Where>

You use QueryGroup.MergeTypes to control where the query is injected and the Types.JoinTypes to dictate the logical operator for that particular query.

2 comments

  1. Thomas says:

    Hi Stuart,

    is there a way to create queries for taxonomy fields using the statement as described here http://msdn.microsoft.com/en-us/library/ff625182.aspx ?

    Kind regards,
    Thomas

    • Stu says:

      Hi Thomas,

      The code would require some small changes but yes it will be possible.

      Basically, all you’re looking for is a query based on a Lookup Type, which is currently possible. The one things that’s missing is the LookupId attribute within the FieldRef element when the value type is set to Lookup.

      Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *

Follow

Get every new post delivered to your Inbox

Join other followers: