Click here to Skip to main content
15,880,543 members
Articles / Productivity Apps and Services / Sharepoint
Tip/Trick

POCO to SharePoint CAML Query

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
27 Jan 2014CPOL 8.6K   1  
POCO to CAML simplifies creating of CAML queries by using expression trees.

Introduction

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.

C#
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(); 

Output

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

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.

C#
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.

History

  • Initial version

License

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


Written By
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.

Comments and Discussions

 
-- There are no messages in this forum --