Click here to Skip to main content
12,633,990 members (24,198 online)
Click here to Skip to main content
Add your own
alternative version

Stats

4.4K views
1 bookmarked
Posted

POCO to SharePoint CAML Query

, 27 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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

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

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)

Share

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 --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 27 Jan 2014
Article Copyright 2014 by Handy Torres
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid