CAML & SPQuery in SharePoint
Using Collaborative Application Markup Language (CAML) queries with SPQuery and SPSiteDataQuery is a faster and more efficient way of retrieving items based on known criteria compare with for each on the SPListItemsCollection and checking for the criteria.
In this tutorial I’m going to discuss how you can write CAML queries for retrieving items in SharePoint List.
Operators
These are some common operators you can use with CAML queries.
Get All Items
This is discussed in SharePoint List C# Part 1. There you can write the query as follows. There you can get all the items in the list.
Single Criteria
Using following query you can get all the tasks where “ID” is equal to 5.
You can get Value Types for different common type of columns using following list
e.g. Use Value Type "Text" for column type "Single line of text".
Using AND
This will give you the items where ID = 5 and Title = "ABC".
OR
This will give you the items where ID = 5 or Title = "ABC"
It is different if you want to use more than one "AND" or "OR" operator. As a example if you want to get items where ID = 5 and Title = "ABC" and created by "John", then you can write this.
In this tutorial I’m going to discuss how you can write CAML queries for retrieving items in SharePoint List.
Operators
These are some common operators you can use with CAML queries.
- Eq--Equals
- Neq--Not equal
- Gt--Greater than
- Geq--Greater than or equal
- Lt--Lower than
- Leq--Lower than
- IsNull--Is null
- BeginsWith--Begins with
- Contains--Contains
Get All Items
This is discussed in SharePoint List C# Part 1. There you can write the query as follows. There you can get all the items in the list.
myquery.Query = "";
Single Criteria
Using following query you can get all the tasks where “ID” is equal to 5.
1
2
3
4
5
6
| < Where > < Eq > < FieldRef Name = 'ID' /> < Value Type = 'Counter' >5</ Value > </ Eq > </ Where > |
e.g. Use Value Type "Text" for column type "Single line of text".
- Single line of text--Text
- Multiple lines of text--Note
- Choice (menu to choose from)--Choice
- Number (1, 1.0, 100)--Number
- Date and Time--DateTime
- Lookup (information already on this site)--Lookup/LookupMulti
- Yes/No (check box)--Boolean
- Person or Group--User
Using AND
This will give you the items where ID = 5 and Title = "ABC".
1
2
3
4
5
6
7
8
9
10
| < Where >< And > < Eq > < FieldRef Name = 'ID' /> < Value Type = 'Counter' >5</ Value > </ Eq > < Eq > < FieldRef Name = 'Title' /> < Value Type = 'Text' >ABC</ Value > </ Eq > </ And ></ Where > |
This will give you the items where ID = 5 or Title = "ABC"
1
2
3
4
5
6
7
8
9
10
| < Where >< OR > < Eq > < FieldRef Name = 'ID' /> < Value Type = 'Counter' >5</ Value > </ Eq > < Eq > < FieldRef Name = 'Title' /> < Value Type = 'Text' >ABC</ Value > </ Eq > </ OR ></ Where > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| < Where > < And > < And > < Eq > < FieldRef Name = 'ID' /> < Value Type = 'Counter' >5</ Value > </ Eq > < Eq > < FieldRef Name = 'Title' /> < Value Type = 'Text' >ABC</ Value > </ Eq > </ And > < Eq > < FieldRef Name = 'Author' /> < Value Type = 'User' >John</ Value > </ Eq > </ And > </ Where > |
No comments:
Post a Comment