Project Description
The SharePoint CAML Helper is a utility class that helps make the process of building CAML queries for SharePoint within .NET code easier, especially for the more complex query.

The source code for this tool is written in C#.

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

Example query using the utility class:

CAMLManager mgr = new CAMLManager(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "List Item Title"));
mgr.QueryGroups.Add(new QueryGroup(Types.FieldRefTypes.ID, "{D562C8AC-457A-446b-84D6-2FA26FAABED7}", Types.FieldTypes.YesNo, Types.QueryTypes.Neq, "true"));

mgr.OrderBy.Add(new OrderBy("Title", false));

mgr.ViewFields.Add(new ViewField("Title"));
mgr.ViewFields.Add(new ViewField("{D562C8AC-457A-446b-84D6-2FA26FAABED7}", Types.FieldRefTypes.ID));

mgr.RowLimit = 100;

string camlQuery = mgr.GetFullCAML();

Another example, with output
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.Contain, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "2"));


string caml = mgr.GetCAML();

Will provide the following:

<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>

    <Geq>

      <FieldRef Name="Total" /><Value Type="Number">2</Value>

    </Geq>

  </And>

</Where>



Different example, with output
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.And, QueryGroup.MergeTypes.BottomGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Eq, "2"));


mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, QueryGroup.MergeTypes.TopGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "3"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.TopGroup, "Total", Types.FieldTypes.Number, Types.QueryTypes.Eq, "4"));

string caml = mgr.GetCAML();


Will provide the following:

<Where>

  <And>

    <And>

      <Or>

        <Or>

          <Eq>

            <FieldRef Name="Title" /><Value Type="Text">A Title</Value>

          </Eq>

          <Eq>

            <FieldRef Name="Title" /><Value Type="Text">Another Title</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>

      <Geq>

        <FieldRef Name="Total" /><Value Type="Number">1</Value>

      </Geq>

      <Eq>

        <FieldRef Name="Total" /><Value Type="Number">2</Value>

      </Eq>

    </And>

  </And>

</Where>

Last edited Oct 4, 2011 at 5:10 AM by stoorob, version 13