Querying from Server-side Code

SharePoint 2010

Last modified: February 02, 2010

Applies to: SharePoint Foundation 2010

In this article
Querying Using CAML and SPQuery
Querying with the LINQ to SharePoint Provider
CAML vs. LINQ

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

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.

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.

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.

Show:
© 2015 Microsoft