Project Description

Dynamically build CAML queries with strongly-typed C# syntax. Never be caught by run-time errors again! If it compiles, then it's valid CAML!

Goals

  1. Correctness - If it compiles, then your Strong Caml query should be a valid CAML query.
    • In other libraries, you'll only find out the query was invalid after deployment!
  2. Completeness - If it's a valid CAML query, then it should be expressible using Strong Caml.
    • Handles CAML's fickle rules. Did you know about the undocumented LookupId="True" attribute for Lookup fields?

Getting Started

To get you started quickly, the Alpha v0.1 release is just a single source file for easy importing.

Just click the download button to the right, include the file in your project, and add a using ChengNet.StrongCaml; to your source.

Examples

Short Example

string validCamlQuery =
    Caml.Query(
        Caml.Where(
            Caml.Eq(
                Caml.FieldRef("Occurrences"),
                Caml.Value(13)                     // ValueType.Integer inferred
            )
        )
    );

string invalidCamlQuery =                          // this won't compile
    Caml.Query(                                    // missing Caml.Where
        Caml.And(                                  // Caml.OrderBy param is the wrong type
            Caml.Eq(
                Caml.FieldRef("User Field"),
                Caml.Value<ValueType.User>("John") // ValueType.User must be
                                                   // an integer value or a Caml.UserID()
                // use ValueType.Text to match account login as string
                // or use ValueType.User/ValueType.Lookup for int SPUser.ID
            ),
            Caml.OrderBy(
                Caml.FieldRef("Occurrences")
            )
        )
    );

Long Example

var camlQuery =
    Caml.Query(
        Caml.Where(
            Caml.And(
                Caml.And(
                    Caml.Lt(
                        Caml.FieldRef("Date"),
                        Caml.Value(DateTime.Now, TimeValue.Include)
                    ),
                    Caml.Geq(
                        Caml.FieldRef("Date"),
                        Caml.Value(
                            Caml.Today(-14)
                        )
                    )
                ),
                Caml.Or(
                    Caml.Eq(
                        Caml.FieldRef("AssignedTo"),
                        Caml.Value(
                            Caml.UserID()
                        )
                    ),
                    Caml.Eq(
                        Caml.FieldRef("Descrip"),
                        Caml.Value<ValueType.Lookup>(13)
                    )
                )
            )
        ),
        Caml.OrderBy(
            Caml.FieldRef("Title", SortOrder.Descending),
            Caml.FieldRef("Author")
        )
    );

string implicitStringQuery = camlQuery;
var prettyQuery = XDocument.Parse(camlQuery.ToString()).ToString();
Console.WriteLine(prettyQuery);

Output

<Query>
  <Where>
    <And>
      <And>
        <Lt>
          <FieldRef Name="Date" />
          <Value Type="DateTime" IncludeTimeValue="TRUE">2012-09-27T14:33:42Z</Value>
        </Lt>
        <Geq>
          <FieldRef Name="Date" />
          <Value Type="DateTime">
            <Today Offset="-14" />
          </Value>
        </Geq>
      </And>
      <Or>
        <Eq>
          <!-- Automatically adds LookupId="True" -->
          <FieldRef Name="AssignedTo" LookupId="TRUE" />
          <Value Type="User">
            <UserID />
          </Value>
        </Eq>
        <Eq>
          <!-- Automatically adds LookupId="True" -->
          <FieldRef Name="Descrip" LookupId="TRUE" />
          <Value Type="Lookup">13</Value>
        </Eq>
      </Or>
    </And>
  </Where>
  <OrderBy>
    <FieldRef Name="Title" Ascending="FALSE" />
    <FieldRef Name="Author" />
  </OrderBy>
</Query>

Last edited Oct 25, 2012 at 12:59 AM by Kache, version 20