This project is read-only.

Employee Sample Class

Sample class used in this documentation. Note: SpAbstract is not required.

Use the SpData attribute to map a property to a SharePoint list column.
  • Name - the column name in SharePoint to map to.
  • ValueType - the type of column (e.g. Choice, Lookup)

public class Employee : SpAbstract
{
  // Map this property to 'Employeed ID' list column in SharePoint.
  [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; }

  // This property maps to a 'Choice' type in SharePoint
  [SpData(ValueType = "Choice")]
  public string Sex { get; set; }

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

  public decimal Salary { get; set; }

  // This property maps to a 'Lookup' type in SharePoint
  [SpData(ValueType = "Lookup")]
  public string Specialization { get; set; }
}

Query Elements

Where

Select
Orderby
Take
Groupby

Additional Features
ViewScope

Where


And / Or Condition

var caml = new Caml<Employee>()
    .Where(
        f => f.LastName == "Smith" && 
        (f.FirstName == "John" || f.FirstName == "Jane"))
    .ToString();

<View>
   <Query>
      <Where>
         <And>
            <Eq>
               <FieldRef Name="Last_x0020_Name" />
               <Value Type="Text">Smith</Value>
            </Eq>
            <Or>
               <Eq>
                  <FieldRef Name="First_x0020_Name" />
                  <Value Type="Text">John</Value>
               </Eq>
               <Eq>
                  <FieldRef Name="First_x0020_Name" />
                  <Value Type="Text">Jane</Value>
               </Eq>
            </Or>
         </And>
      </Where>
   </Query>
</View>



Additional And / Or Condition - If you need to add additional filters to the existing 'where' condition(s)

var caml = new Caml<Employee>()
    .Where(f => f.LastName == "Smith");

if (includeSalaryInFilter)
    caml.AndAlso(f => f.Salary > salary);

if (includeYearHired)
    caml.OrElse(f => f.YearHired > yearHired);


Without a 'Where' clause

var caml = new Caml<Employee>()
    .Select(s => new {s.FirstName, s.LastName});

if (includeSalaryInFilter)
    caml.AndAlso(f => f.Salary > salary);

if (includeYearHired)
    caml.OrElse(f => f.YearHired > yearHired);



Eq (Equal To)

var caml = new Caml<Employee>()
    .Where(f => f.EmployeeId == 1)
    .ToString();

<View>
   <Query>
      <Where>
         <Eq>
            <FieldRef Name="Employee_x0020_ID" />
            <Value Type="Integer">1</Value>
         </Eq>
      </Where>
   </Query>
</View>



Neq (Not Equal To)

var caml = new Caml<Employee>()
    .Where(f => f.EmployeeId != 1)
    .ToString();

<View>
   <Query>
      <Where>
         <Neq>
            <FieldRef Name="Employee_x0020_ID" />
            <Value Type="Integer">1</Value>
         </Neq>
      </Where>
   </Query>
</View>



Geq and Leq (Greater Than or Equal To / Less Than or Equal To)

var caml = new Caml<Employee>()
    .Where(f => f.Salary >= 3500 && f.Salary <= 4000)
    .ToString();

<View>
   <Query>
      <Where>
         <And>
            <Geq>
               <FieldRef Name="Salary" />
               <Value Type="Currency">3500</Value>
            </Geq>
            <Leq>
               <FieldRef Name="Salary" />
               <Value Type="Currency">4000</Value>
            </Leq>
         </And>
      </Where>
   </Query>
</View>



Gt and Lt (Greater Than / Less Than)

var caml = new Caml<Employee>()
    .Where(
        f => 
        f.YearHired > DateTime.Now.AddYears(-5).Year &&
        f.YearHired < DateTime.Now.AddYears(-1).Year
        )
    .ToString();

<View>
   <Query>
      <Where>
         <And>
            <Gt>
               <FieldRef Name="Year_x0020_Hired" />
               <Value Type="Integer">2009</Value>
            </Gt>
            <Lt>
               <FieldRef Name="Year_x0020_Hired" />
               <Value Type="Integer">2013</Value>
            </Lt>
         </And>
      </Where>
   </Query>
</View>



IsNotNull

var caml = new Caml<Employee>()
    .Where(f => f.Title != null)
    .ToString();

<View>
   <Query>
      <Where>
         <IsNotNull>
            <FieldRef Name="Title" />
         </IsNotNull>
      </Where>
   </Query>
</View>



IsNull

var caml = new Caml<Employee>()
    .Where(f => f.Title == null)
    .ToString();

<View>
   <Query>
      <Where>
         <IsNull>
            <FieldRef Name="Title" />
         </IsNull>
      </Where>
   </Query>
</View>



BeginsWith (String.StartsWith)

var caml = new Caml<Employee>()
    .Where(f => f.LastName.StartsWith("T"))
    .ToString();

<View>
   <Query>
      <Where>
         <BeginsWith>
            <FieldRef Name="Last_x0020_Name" />
            <Value Type="Text">T</Value>
         </BeginsWith>
      </Where>
   </Query>
</View>



Contains (String.Contains)

var caml = new Caml<Employee>()
    .Where(f => f.FirstName.Contains("John"))
    .ToString();

<View>
   <Query>
      <Where>
         <Contains>
            <FieldRef Name="First_x0020_Name" />
            <Value Type="Text">John</Value>
         </Contains>
      </Where>
   </Query>
</View>



In
  • Works on IEnumerable<T>
  • string[], int[], etc.

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

var caml = new Caml<Employee>()
    .Where(f => departments.Contains(f.Department))
    .ToString();

<View>
   <Query>
      <Where>
         <In>
            <FieldRef Name="Department" />
            <Values>
               <Value Type="Text">Information Technology</Value>
               <Value Type="Text">Sales</Value>
            </Values>
         </In>
      </Where>
   </Query>
</View>



UserID

Assign the SpElement.UserID value to a property.

var caml = new Caml<Employee>()
    .Where(
        f => 
        f.AuthorId == SpElement.UserID || 
        f.EditorId == SpElement.UserID)
    .ToString();

<View>
   <Query>
      <Where>
         <Or>
            <Eq>
               <FieldRef Name="Author" />
               <Value Type="Integer">
                  <UserID />
               </Value>
            </Eq>
            <Eq>
               <FieldRef Name="Editor" />
               <Value Type="Integer">
                  <UserID />
               </Value>
            </Eq>
         </Or>
      </Where>
   </Query>
</View>



DateRangesOverlap

var caml = new Caml<Calendar>()
    .Where(f => SpElement.DateRangesOverlap(f, s => s.EventDate, e => e.EndDate, r => r.RecurrenceId, DateRange.Now))
    .ToString();

<View>
   <Query>
      <Where>
         <DateRangesOverlap>
            <FieldRef Name="EventDate" />
            <FieldRef Name="EndDate" />
            <FieldRef Name="RecurrenceID" />
            <Value Type="DateTime">
               <Now />
            </Value>
         </DateRangesOverlap>
      </Where>
   </Query>
</View>



Includes

Use the Includes extension method (Namespace: CodeToCaml.Main.SpExtension):

var caml = new Caml<Employee>()
    .Where(f => f.Specialization.Includes("Programming"))
    .ToString();

<View>
   <Query>
      <Where>
         <Includes>
            <FieldRef Name="Specialization" />
            <Value Type="Lookup">Programming</Value>
         </Includes>
      </Where>
   </Query>
</View>



NotIncludes

var caml = new Caml<Employee>()
    .Where(f => f.Specialization.NotIncludes("Marketing"))
    .ToString();

<View>
   <Query>
      <Where>
         <NotIncludes>
            <FieldRef Name="Specialization" />
            <Value Type="Lookup">Marketing</Value>
         </NotIncludes>
      </Where>
   </Query>
</View>



Membership

var caml = new Caml<Employee>()
    .Where(
        f => SpElement.Membership(f, p => p.AuthorId, MembershipType.CurrentUserGroups) ||
        f.AuthorId == SpElement.UserID)
    .ToString();

<View>
   <Query>
      <Where>
         <Or>
            <Membership Name="CurrentUserGroups">
               <FieldRef Name="Author" />
            </Membership>
            <Eq>
               <FieldRef Name="Author" />
               <Value Type="Integer">
                  <UserID />
               </Value>
            </Eq>
         </Or>
      </Where>
   </Query>
</View>


Select

Transforms the given expression into ViewFields Element.

var caml = new Caml<Employee>()
    .Select(s => new {s.FirstName, s.LastName, s.Department})
    .Where(f => f.YearHired > 2005)
    .ToString();

<View>
   <ViewFields>
      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
   </ViewFields>
   <Query>
      <Where>
         <Gt>
            <FieldRef Name="Year_x0020_Hired" />
            <Value Type="Integer">2005</Value>
         </Gt>
      </Where>
   </Query>
</View>


OrderBy

Determines the sort order for a query.
  • OrderBy
  • OrderByDescending
  • ThenBy
  • ThenByDescending

var caml = new Caml<Employee>()
    .Select(s => new {s.FirstName, s.LastName, s.Department})
    .Where(f => f.YearHired > 2005)
    .OrderBy(o => o.LastName)
    .ThenBy(o => o.FirstName)
    .ToString();

<View>
   <ViewFields>
      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
   </ViewFields>
   <Query>
      <Where>
         <Gt>
            <FieldRef Name="Year_x0020_Hired" />
            <Value Type="Integer">2005</Value>
         </Gt>
      </Where>
      <OrderBy>
         <FieldRef Name="Last_x0020_Name" Ascending="True" />
         <FieldRef Name="First_x0020_Name" Ascending="True" />
      </OrderBy>
   </Query>
</View>


Take (RowLimit Element)

Sets the query row limit.

var caml = new Caml<Employee>()
    .Select(s => new {s.FirstName, s.LastName, s.Department})
    .Where(f => f.Salary > 5000)
    .OrderByDescending(o => o.Salary)
    .Take(50)
    .ToString();

<View>
   <ViewFields>
      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
   </ViewFields>
   <Query>
      <Where>
         <Gt>
            <FieldRef Name="Salary" />
            <Value Type="Number">5000</Value>
         </Gt>
      </Where>
      <OrderBy>
         <FieldRef Name="Salary" Ascending="False" />
      </OrderBy>
   </Query>
   <RowLimit>50</RowLimit>
</View>


GroupBy

Sets the GroupBy query element.
  • GroupBy
  • GroupByCollapse - Use this to set the Collapse attribute to "True"

var caml = new Caml<Employee>()
    .Select(s => new { s.YearHired })
    .Where(f => f.YearHired > 2000)
    .GroupBy(g => g.YearHired)
    .ToString();

<View>
   <ViewFields>
      <FieldRef Name="Year_x0020_Hired" />
   </ViewFields>
   <Query>
      <Where>
         <Gt>
            <FieldRef Name="Year_x0020_Hired" />
            <Value Type="Integer">2000</Value>
         </Gt>
      </Where>
      <GroupBy>
         <FieldRef Name="Year_x0020_Hired" />
      </GroupBy>
   </Query>
</View>


Additional Features

Moderation Status - Content Approval

Add a property with the following attribute to your class:

[SpData(Name = "_ModerationStatus", ValueType = "ModStat")]
public SpModerationStatus ModerationStatus { get; set; }


var caml = new Caml<Employee>()
    .Select(s => new { s.FirstName, s.LastName, s.Department })
    .Where(f => f.ModerationStatus == SpModerationStatus.Approved)
    .OrderBy(o => o.LastName)
    .ThenBy(o => o.FirstName)
    .ToString();

<View>
   <ViewFields>
      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
   </ViewFields>
   <Query>
      <Where>
         <Eq>
            <FieldRef Name="_ModerationStatus" />
            <Value Type="ModStat">0</Value>
         </Eq>
      </Where>
      <OrderBy>
         <FieldRef Name="Last_x0020_Name" Ascending="True" />
         <FieldRef Name="First_x0020_Name" Ascending="True" />
      </OrderBy>
   </Query>
</View>


View Scope

var caml = new Caml<Employee> {ViewScope = SpViewScope.RecursiveAll}
    .Where(f => f.EmployeeId == 1)
    .ToString();

<View Scope="RecursiveAll">
   <Query>
      <Where>
         <Eq>
            <FieldRef Name="Employee_x0020_ID" />
            <Value Type="Integer">1</Value>
         </Eq>
      </Where>
   </Query>
</View>

Last edited Nov 11, 2015 at 12:58 PM by HTT323, version 20