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"></fieldref>
      <value Type="Text">A Title</value>
    </eq>
    <eq>
      <fieldref Name="Title"></fieldref>
      <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.

This entry was posted in SharePoint and tagged , . Bookmark the permalink.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

2 Comments
Inline Feedbacks
View all comments
Thomas

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