Skip Navigation LinksHome > Articles > Sharepoint > CAML Query syntax and options in SharePoint

CAML Query syntax and options in SharePoint

Explains About CAML Query in Sharepoint

By Project 9X   On   Tuesday, 09 September 2008

Page Views : 31023   |   Technologies : Sharepoint

Rating : Rated :
0

As Small syntax error or order of tag in spquery can result any web part (or any thing in which spquery is used) doesn’t work, I’m explaining SPQuery with example, hope which may help you.

CAML (Collaborative Application Markup Language)

SPQuery is the SharePoint object which is used to perform a query operation against SharePoint data.SPList.getItems(SPQuery) is the step, will return SPListItemCollection which satisfies the query.SPQuery has one data member ‘Query’, which need to set before passing SPQuery object to SPList.

Some Properties

Properties

Description

RowLimit

Gets or sets a limit for the number of items returned in the query per page.

<RowLimit>10</RowLimit>
---
Can be used as: query.rowLimit = 10;

ViewFields

Gets or sets the fields that are displayed in the query.

<ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Name'/>
</ViewFields>
---
Can be used as:
 SPQuery query = new SPQuery();
 query.ViewFields = "<FieldRef Name='Field1'/>" +
                      "<FieldRef Name='Field2'/>";   

How to Implement SPQuery?

How to Implement SPQuery?

using (SPWeb web = SPContext.Current.Site.RootWeb)
{
    SPList mylist = web.Lists["Tasks"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='Status'/>" +
                   "<Value Type='Text'>Completed</Value></Eq></Where>";
    SPListItemCollection items = mylist.GetItems(query);
    foreach (SPListItem item in items)
    {
        Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
    }
}

Operators:

Comparison Operators, Logical Joins and Order/ Group Operators, plays an important role to make this syntax.

 

Comparison Operators

Comparison Operators

General Meaning

Eq

=

Gt

> 

Lt

< 

Geq

>=

Leq

<=

Neq

<> 

Contains

Like

IsNull

Null

IsNotNull

NotNull

BeginsWith

Beginning with word

DateRangesOverlap

compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap

 

Now, to use Or between Geq and Leq conditions, we put it inside <Or> tag.

I.e. condition: where (Field1 >= 1500) or (field2 <= 500) can be written like this:

   <Where>
  <Or>
     <Geq>
         <FieldRef Name='Field1'/>
         <Value Type='Number'>1500</Value>
     </Geq>
     <Leq>
        <FieldRef Name='Field2'/><Value Type='Number'>500</Value>
     </Leq>
   </Or>
  </Where>

The full Code snippets will look like:

<View>
 <Query>
      <OrderBy>
          <FieldRef Name='ID'/>
      </OrderBy>
 
  <Where>
  <Or>
     <Geq>
         <FieldRef Name='Field1'/>
         <Value Type='Number'>1500</Value>
     </Geq>
     <Leq>
        <FieldRef Name='Field2'/><Value Type='Number'>500</Value>
     </Leq>
   </Or>
  </Where>
 </Query>
   <ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Name'/>
   </ViewFields>
   <RowLimit>10</RowLimit>
</View>

Example: Contains, And, BeginsWith

The following example uses the Contains element that is assigned to the Query property to return the titles of items where the Conference column value begins with "Morning" and contains "discussion session".

<Where>
   <And>
       <BeginsWith>
              <FieldRef Name="Conference"/> 
              <Value Type="Note">Morning</Value>
       </BeginsWith>
       <Contains>
              <FieldRef Name="Conference" />
              <Value Type="Note">discussion session</Value>
       </Contains>
   </And>
</Where>

 

Example: DateRangesOverlap

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

Logical Joins:

Logical Joins

Comments

And

Used within the ‘Where’ element to group filters in a query for a view

Or

Used within the ‘Where’ element to group filters in a query for a view

Example: And

<Where>
  
<And>
     <Neq>
        <FieldRef Name="Status"></FieldRef>
        <Value Type="Text">Completed</Value>
     </Neq>
     <IsNull>
        <FieldRef Name="Sent"></FieldRef>
     </IsNull>
  
</And>
</Where>

Order/Group Operators:

Order/Group Operators

Comments

OrderBy

Determines the sort order for a query. The OrderBy element contains a group of FieldRef elements

I.e. <OrderBy><FieldRef Name="Title" Ascending="TRUE">

</FieldRef></OrderBy>

 

GroupBy

Contains a Group By section for grouping the data returned through a query in a list view

Example: OrderBy

<OrderBy> 
       <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
<Where>
  <Or>
    <Neq>
      <FieldRef Name="Status"></FieldRef>
      <Value Type="Text">Completed</Value>
    </Neq>
    <IsNull>
      <FieldRef Name="Status"></FieldRef>
    </IsNull>
  </Or>
</Where>

Example: GroupBy

<GroupBy> 
      <FieldRef Name="Modified"/>
</GroupBy>
<Where>
   <Or>
      <Neq>
          <FieldRef Name="Status"></FieldRef>
          <Value Type="Text">Completed</Value>
      </Neq>
      <IsNull>
          <FieldRef Name="Status"></FieldRef>
      </IsNull>
   </Or>
</Where>

Important:

This is very important thing to keep in mind, because SPQuery will be assigned as a string so it wont show compile time error though it may have tag syntax error, not in order, escape sequence error, etc.

Escape sequence:

The ScriptEncode method escapes characters that would otherwise conflict with script.

Original

Replacement

"

\"

\

\\

+

\u002b

> 

\u003e

< 

\u003c

'

\u0027

 


 

Example:

<FieldRef Name=\"Checkbox\"></FieldRef> <Value Type=\"bit\">1</Value>

Thanks


 

 


Keywords :
Tags :
Rate This Article :

Comments :

# 1 Annonymous Wrote on 12/01/2008


Nice Article



# 2 Annonymous Wrote on 02/04/2009


good



# 3 Annonymous Wrote on 02/09/2009


Very Good Article



# 4 Annonymous Wrote on 02/19/2009


Excellent Article



# 5 Annonymous Wrote on 03/03/2009


Nice article.. Is there any operator/property like DISTINCT in CAML ? So that i can get the unique items of a particular column of a list.



# 6 Annonymous Wrote on 03/08/2009


Thanks. Can you give an example where the AND and OR operators are grouped? e.g. (A OR B) AND (C OR D). Intuitively this should be and or A B /or or C D /or /and but it doesn't seem to work...



# 7 Annonymous Wrote on 03/13/2009


Very helpful! Thanks, Ash



# 8 Annonymous Wrote on 03/16/2009


Really gr8 article....it is very helpful.. Thanx....... Regards, Manvendra



# 9 Annonymous Wrote on 04/08/2009


Thank you for this article. Can CAML take a parameter? What woudl be the syntax for passing a parameter into a caml query? thanks again.



# 10 Annonymous Wrote on 04/16/2009


i have a question. CAML have more operater like "IN" or "BETWEEN".or can we do nested query



# 11 Annonymous Wrote on 05/11/2009


very goog,need to be appreciate



# 12 Annonymous Wrote on 06/15/2009


gr888 article... thnx for sharing ur learning... keep it up



# 13 Annonymous Wrote on 06/22/2009


is there any ways of reading only unique or distinct values from a column?



# 14 Annonymous Wrote on 07/14/2009


Excellent Article..Thanks a lot..



# 15 Annonymous Wrote on 07/21/2009


nice artical... but still i have one confusion in u2u query and that is how i can join two or more tables data in one query in sharepoint... Vicky Sharma



# 16 Annonymous Wrote on 07/30/2009


very nice article, very helpful for understanding CAML



# 17 Annonymous Wrote on 09/02/2009


Excellent!



# 18 Annonymous Wrote on 09/09/2009


Very good article expecting more on this..



# 19 Annonymous Wrote on 11/09/2009


Excellent



# 20 Annonymous Wrote on 11/13/2009


nice artical



# 21 Annonymous Wrote on 11/13/2009


Great



# 22 Annonymous Wrote on 11/16/2009


nic



# 23 Annonymous Wrote on 11/19/2009


Excellent Article. Thanks for sharing your knowledge.



# 24 Annonymous Wrote on 11/23/2009


very Nice article



# 25 Annonymous Wrote on 12/07/2009


Very Helpful....Thanks...



# 26 Annonymous Wrote on 12/21/2009


This article is helpful for me, Thank You



# 27 Annonymous Wrote on 01/04/2010


Super Article for the beginers



# 28 Annonymous Wrote on 01/05/2010


This is a very good article for beginners like me. Thanks



# 29 Annonymous Wrote on 01/06/2010


the best tutorial I found on Internet



# 30 girishkumar518 Wrote on 04/08/2010


Thanq very good article



Write a Comment / Question / Feedback ...


User Login
Username :
Password :
Register Login

Forgot Password


Related Articles