Querying from Server-side Code

Applies to: SharePoint Foundation 2010

This topic provides an overview of the differences between the two ways of querying Microsoft SharePoint Foundation data in server-side code.

Querying Using CAML and SPQuery

You can query any kind of SharePoint Foundation data, including non-list data, by using the regular server object model. The most important class for querying is SPQuery. Parts of queries written in Collaborative Application Markup Language (CAML) are assigned to the Query, Joins, ProjectedFields, and ViewFields properties of an SPQuery object. The object is then passed to a method such as SPList.GetItems() which returns the queried items.

For examples, see Working with List Objects and Collections and How to: Return Items from a List.

Querying with the LINQ to SharePoint Provider

You can query SharePoint Foundation list data, and only list data, by using the LINQ to SharePoint provider. You write queries in LINQ syntax and assign the results to a return object. The query is executed the first time the return object is enumerated. At that time, the query is translated by the LINQ to SharePoint provider into a CAML query and then processed as any other CAML query. For more information, see the topics under the node Managing Data with LINQ to SharePoint.

CAML vs. LINQ

The principal advantages of CAML queries is that there may be a performance advantage because there is no need to translate the query from LINQ syntax to CAML syntax.

The principal advantages of using LINQ queries include:

  • You can use LINQ syntax and the LINQ keywords built into C# and Microsoft Visual Basic. By contrast, CAML XML is error prone and applicable only to SharePoint Foundation.

  • With the LINQ to SharePoint provider, you are working with strongly typed list item objects. In the SharePoint Foundation client object model, any two SPListItem objects are the same type, even if they represent items from dramatically different kinds of lists. However, the LINQ to SharePoint provider uses entity classes, including a class for each content type used on any list in a Web site. For example, an item in the Announcements list is an object of type Announcement and an item on a Tasks list is an object of type Task.

See Also

Reference

Query Element (List)

Concepts

Query Schema

Querying from Client-side Code