Click here to Skip to main content
Licence GPL3
First Posted 22 Mar 2008
Views 84,555
Bookmarked 17 times

Understanding Sharepoint CAML query

By | 22 Mar 2008 | Article
How to quickly write CAML Query in Sharepoint

Introduction

Sharepoint CAML query is difficult to understand. Here I have provided a quick method to create CAML query.

Sharepoint

Caml Query

  • A caml query is build in two parts.
    • 1) Sort
    • 2) Filter
  • Sort part is the sorting based on some columns
  • Filter part is the where clause of the CAML query
  • In order to build up the XML for the CAML we create the where clause as follows:
    • Write the where clause just as you write in sql
    • Find out the operators such as =, <=, <,> etc.
    • Find out the keywords for these operators in CAML language. For example = is eq, < is lt and so on.
    • Break the sql string such that the operator comes first and then comes the operands. Continue this till the whole string is completed in this fashion.
    • Replace the operators with the keywords in CAML language.
    • Example: where column1=”a” and column2=”b” and column3 like ‘%c%’
    • => where (column1=”a” and column2=”b”) and column3 like ‘%c%’
    • => where and (column1=”a” and column2=”b”) (column3 like ‘%c%’)
    • => where and (and (column1=”a” column2=”b”) column3 like ‘%c%’)
    • => where and and (= column1 a column2 b) contains column3 c
    • => where and and eq column1 a eq column2 b contains column3 c
    • => <where>

<and>

          <and>

                   <eq>

                             <FieldRef name=”Column1” />

                             <Value Type=string>a</Value>

                   </eq>

                   <eq>

                             <FieldRef name=”Column2” />

                             <Value Type=string>b</Value>                                 

 </eq>

          </and>

          <contains>

                   <FieldRef name=”Column3” />

                   <Value Type=string>c</Value>

          </contains>

</and>

                           </where>

Points of Interest

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Sushant Agrawal

Web Developer
Netwoven
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmvpMark Nischalke15:50 2 Jan '12  
GeneralMy vote of 1 PinmemberOslec21:51 27 Apr '11  
QuestionWhat a waste of space Pinmemberrobvon19:42 12 Nov '10  
AnswerRe: What a waste of space PinmemberRobert Miscellaneous4:18 14 Dec '10  
GeneralMy vote of 5 PinmemberManu Agrawal7:26 8 Nov '10  
GeneralMy vote of 1 PinmemberAmir Mehrabi-Jorshary10:36 27 Sep '10  
GeneralMy vote of 4 Pinmemberam xaml20:42 8 Aug '10  
GeneralWeb Part that saves writing CAML PinmemberKunal Bagga22:21 24 Oct '09  
GeneralUse CAML in SharePoint is simple Pinmemberitsaranga6:36 3 Oct '09  
GeneralMy vote of 2 PinmemberMember 116629322:13 30 Jul '09  
GeneralMy vote of 1 Pinmemberrhino1795:13 2 Jul '09  
Generalcreate view Pinmemberjoekey4:34 3 Apr '09  
GeneralMy vote of 1 Pinmemberscottgp6:17 11 Mar '09  
GeneralUsing CAML query to inner join two Sharepoint lists PinmemberJinto Jose5:32 26 Sep '08  
GeneralRe: Using CAML query to inner join two Sharepoint lists Pinmemberpartyganger11:26 12 Dec '08  
GeneralRe: Using CAML query to inner join two Sharepoint lists PinmemberSushant Agrawal17:13 12 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Mar 2008
Article Copyright 2008 by Sushant Agrawal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid