SharePoint 2013 - SPLessons

SharePoint CAML Queries

Home > Lesson > Chapter 30
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SharePoint CAML Queries

SharePoint CAML Queries

The Query schema of Collaborative Application Markup Language (CAML) is used in various ways within the context of Microsoft SharePoint to define queries against list data.It is an XML-based language that is used in Microsoft Windows SharePoint Services to define sites and lists for E.g. fields, views, or forms etc. Developers mostly use CAML to write Queries to retrieve data from Lists\libraries. you can assign CAML query strings to the Query property of several classes in the server and client object models in order to return data, or to method parameters of the Lists web service to work remotely with data. CAML queries are also used in the context of CAML views to return specific data. The Query element of the List schema contains elements of the Query schema.

Syntax

[xml]</pre> SPQuery query = new SPQuery(); query.Viewfields = @"<FieldRef Name='Title'/><FieldRef Name='Location'/>"; query.Query = @"<Where> <Eq> <FieldRef Name='Location'/> <Value Type='Text'>Texas</Value> </Eq> </Where>"; SPList list = SPContext.Current.Web.Lists.TryGetList("Announcements"); SPListItemCollections items = list.GetItems(query); <pre>[/xml]

Example

[xml] <Query><Where> <And> <Eq> <FieldRef Name='EmployeeID' /> <Value Type='Id' >5151</Value> </Eq> <And> <Eq> <FieldRef Name='EmployeName' /> <Value Type='Text' >Sreehari</Value> </Eq> </And> </And> </Where> <OrderBy> <FieldRef Name='ExpenseDate' Ascending='True' /> </OrderBy> </Query> [/xml]