This project is read-only.
Project Description
POCO to CAML simplifies creation of SharePoint CAML queries by using expression trees.

Version 1.2.0.0 now out in Nuget.

Moving to GitHub soon!

Updates
  • Nov 11 2015 - Hotfix - fixed bug in AndAlso / OrElse.
  • Nov 10 2015 - Added support for AndAlso and OrElse.
  • Feb 06 2014 - Added support for Nullable<> type.
  • Feb 05 2014 - Changed bool value from "true" to 1 and "false" to 0.

Usage
Documentation: https://camlexpression.codeplex.com/documentation

Installation Instructions
NuGet package: https://www.nuget.org/packages/CodeToCaml/

Example

var departments = new[] { "Information Technology", "Sales" };

var caml = new Caml<Employee>()
    .Select(s => new { s.FirstName, s.LastName, s.Department, s.Salary })
    .Where(
        f => 
        f.LastName == "Smith" && 
        (f.FirstName.StartsWith("J") || f.FirstName.Contains("John")) && 
        f.YearHired > DateTime.Now.AddYears(-5).Year &&
        f.Salary < 2500 &&
        departments.Contains(f.Department) &&
        f.ModerationStatus == SpModerationStatus.Approved)
    .OrderBy(o => o.LastName)
    .ThenBy(o => o.FirstName)
    .ToString();


CAML query output using the ToString() method:

Using data annotations applied to properties, field names and types are converted to SharePoint equivalent.

<View>
   <ViewFields>
      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
      <FieldRef Name="Salary" />
   </ViewFields>
   <Query>
      <Where>
         <And>
            <And>
               <And>
                  <And>
                     <And>
                        <Eq>
                           <FieldRef Name="Last_x0020_Name" />
                           <Value Type="Text">Smith</Value>
                        </Eq>
                        <Or>
                           <BeginsWith>
                              <FieldRef Name="First_x0020_Name" />
                              <Value Type="Text">J</Value>
                           </BeginsWith>
                           <Contains>
                              <FieldRef Name="First_x0020_Name" />
                              <Value Type="Text">John</Value>
                           </Contains>
                        </Or>
                     </And>
                     <Gt>
                        <FieldRef Name="Year_x0020_Hired" />
                        <Value Type="Integer">2009</Value>
                     </Gt>
                  </And>
                  <Lt>
                     <FieldRef Name="Salary" />
                     <Value Type="Number">2500</Value>
                  </Lt>
               </And>
               <In>
                  <FieldRef Name="Department" />
                  <Values>
                     <Value Type="Text">Information Technology</Value>
                     <Value Type="Text">Sales</Value>
                  </Values>
               </In>
            </And>
            <Eq>
               <FieldRef Name="_ModerationStatus" />
               <Value Type="ModStat">0</Value>
            </Eq>
         </And>
      </Where>
      <OrderBy>
         <FieldRef Name="Last_x0020_Name" Ascending="True" />
         <FieldRef Name="First_x0020_Name" Ascending="True" />
      </OrderBy>
   </Query>
</View>


Employee - Sample Class
  • Note: SpAbstract is not required

The Employee class inherits from SpAbstract (with common fields e.g. Title, Author, Editor, etc.) and members are decorated with SpData attributes to map to SharePoint fields and types.

public class Employee : SpAbstract
{
  [SpData(Name = "Employee ID")]
  public int EmployeeId { get; set; }

  [SpData(Name = "First Name")]
  public string FirstName { get; set; }

  [SpData(Name = "Last Name")]
  public string LastName { get; set; }

  public string Department { get; set; }

  [SpData(Name = "Birth Date")]
  public DateTime DateOfBirth { get; set; }

  [SpData(ValueType = "Choice")]
  public string Sex { get; set; }

  [SpData(Name = "Year Hired")]
  public int YearHired { get; set; }

  public decimal Salary { get; set; }

  [SpData(ValueType = "Lookup")]
  public string Specialization { get; set; }
}

Last edited May 25 at 1:45 PM by HTT323, version 41