SPQuery Class
Represents a query in a list view.
Namespace:
Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
The Type attribute is particularly useful for Lookup and User fields. Otherwise, without that attribute, the query may not return the expected results. In some cases, the query may fail completely.
For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.
The following code example displays the titles of items in a Tasks list where the Status column equals Completed. The example uses Collaborative Application Markup Language (CAML) to define the query.
This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.
SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb SPList oList = oWebsiteRoot.Lists["Tasks"]; SPQuery oQuery = new SPQuery(); oQuery.Query = "<Where><Eq><FieldRef Name='Status'/>" + "<Value Type='Text'>Completed</Value></Eq></Where>"; SPListItemCollection collListItems = oList.GetItems(oQuery); foreach (SPListItem oListItem in collListItems) { Response.Write(SPEncode.HtmlEncode(oListItem["Title"].ToString()) + "<BR>"); }
Disposal
Note: An SPWeb object obtained through the SPContext object should not be disposed. This code sample was be corrected in the most recent version of the SharePoint SDK to reflect that.
- 1/12/2011
- Binary Jam
- 2/9/2012
- Tom Resing MCM
Query items by rating values using CAML
In this post I illustrate through examples how you can use the rating average and the number of the rates in CAML query:
http://pholpar.wordpress.com/2010/02/03/query-items-by-rating-values-using-caml/
http://pholpar.wordpress.com/2010/02/03/query-items-by-rating-values-using-caml/
- 2/3/2010
- Peter Holpar
CAML query on metadata using a TaxonomyFieldValue or Term
If you would like to know how to query items using CAML based on managed metadata taxonomies, see this post:
http://pholpar.wordpress.com/2010/02/03/some-words-about-taxonomyfieldvalue-and-its-wssid-property/
http://pholpar.wordpress.com/2010/02/03/some-words-about-taxonomyfieldvalue-and-its-wssid-property/
- 2/3/2010
- Peter Holpar
- 2/3/2010
- Peter Holpar
