Click here to Skip to main content
13,346,487 members (65,570 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 27 Jan 2014

POCO to SharePoint CAML Query

, 27 Jan 2014
Rate this:
Please Sign up or sign in to vote.
POCO to CAML simplifies creating of CAML queries by using expression trees.


POCO to CAML is a tool to help you build complex SharePoint CAML queries using expression trees.

Project Information

For a complete list of supported query features and elements, please check the project documentation page and source code hosted here.

Using the Code

Using POCO (Plain Old CLR Object) decorated with attributes to map SharePoint field names and types, and arbitrary expression trees, simply call the ToString() method to get the generated CAML.

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

var caml = new Caml<Employee>()
    .Select(s => new { s.FirstName, s.LastName, s.Department, s.Salary })
        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)


      <FieldRef Name="First_x0020_Name" />
      <FieldRef Name="Last_x0020_Name" />
      <FieldRef Name="Department" />
      <FieldRef Name="Salary" />
                           <FieldRef Name="Last_x0020_Name" />
                           <Value Type="Text">Smith</Value>
                              <FieldRef Name="First_x0020_Name" />
                              <Value Type="Text">J</Value>
                              <FieldRef Name="First_x0020_Name" />
                              <Value Type="Text">John</Value>
                        <FieldRef Name="Year_x0020_Hired" />
                        <Value Type="Integer">2009</Value>
                     <FieldRef Name="Salary" />
                     <Value Type="Number">2500</Value>
                  <FieldRef Name="Department" />
                     <Value Type="Text">Information Technology</Value>
                     <Value Type="Text">Sales</Value>
               <FieldRef Name="_ModerationStatus" />
               <Value Type="ModStat">0</Value>
         <FieldRef Name="Last_x0020_Name" Ascending="True" />
         <FieldRef Name="First_x0020_Name" Ascending="True" />

The Employee class from the above example inherits from SpAbstract (included in the project) and properties decorated with SpDataAttribute to map SharePoint field names 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; }

Feature Requests

Feel free to request for features which you think are vital and are missing. Post in the discussion board or message me here on CodeProject.


  • Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Handy Torres
Software Developer (Senior) JLT Interactive Pte Ltd
Singapore Singapore
Handy Torres is a .NET developer from the Philippines currently living in Singapore and working for JLT Interactive Private Limited as Senior IT Consultant.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 27 Jan 2014
Article Copyright 2014 by Handy Torres
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid