Building Block: Queries and Views

Applies to: SharePoint Foundation 2010

In Microsoft SharePoint Foundation 2010, a query determines what site data or list data is returned, while a view determines how that data is displayed within the page. A view always contains a query, but a query is not always associated with a view, because a query can be used independently of a view in a managed code context. Views can be shared as view styles across lists in a Web site.

Collaborative Application Markup Language (CAML) has traditionally served as the principal means for defining queries and views in SharePoint Foundation. You can use a CAML string to define a query within the context of the server object model or the new client object model. However, SharePoint Foundation 2010 introduces an alternative method to define queries that does not require using CAML. Instead, you can now query SharePoint data directly by using the new LINQ to SharePoint provider, which allows you to query lists from server code that uses Language Integrated Query (LINQ) syntax. SPMetal is a command-line tool you can use to generate entity classes, which in turn provide an object oriented interface to the SharePoint Foundation content databases.

Object Model for Queries and Views

When working with server code that uses the Microsoft.SharePoint namespace, you define a query by instantiating an SPQuery object, and assigning a string containing CAML that defines the query to the Query property. You then pass the SPQuery object as parameter in the GetItems() method of the SPList object to return specified list items.

Client code that uses the new Microsoft.SharePoint.Client namespace (JavaScript: SP Namespace) works in a similar way. In client code, you can define a query by instantiating a CamlQuery object (JavaScript: CamlQuery) and assigning a CAML query string to the ViewXml property (JavaScript: viewXml). You then pass the CamlQuery as parameter in the GetItems(CamlQuery) method (JavaScript: getItems) of List (JavaScript: List) to return specified list items.

The most important classes to use when working with views and queries in the server and client object models include the following:

For information about using queries in the server object model, see How to: Return Items from a List. For information about using queries in the client object model, see Data Retrieval Overview and How to: Retrieve List Items.

XML Used for Queries and Views

The CAML View schema contains a CAML query, although the Query schema is often used in contexts outside of the View schema. A view is contained within the Schema.xml file for a list, which is located in the %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURE folder for a list definition. In SharePoint Foundation 2010, much of the view is defined through XSLT in .xsl files located in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\XSL. For information about XSLT views, see List Views. SharePoint Foundation 2010 introduces new Joins and ProjectFields elements that the View element can contain, which allows a list view to include fields from other lists that have been joined to the primary list.

For information about the structure of the View and Query schemas and their elements, see View Schema and Query Schema.

Object Model for LINQ to SharePoint Provider

The Microsoft.SharePoint.Linq namespace provides the following important classes that are used to implement the LINQ to SharePoint provider.

  • DataContext - Main gateway class that provides access to SharePoint Foundation and the functionality for LINQ querying, writing to the content databases, and object change management. GetList<T>(String) returns an EntityList<TEntity> object that represents a list that can be queried, and SubmitChanges() writes changes to the content database.

  • EntityList<TEntity> – Represents a list that can be queried by using LINQ. Together with a set of other "entity classes" that represent list items and field values, this list provides an object-relational mapping and interface between object-oriented .NET code and the relational structure of content databases.

  • EntityRef<TEntity> – Entity class that provides for deferred loading and relationship maintenance for the singleton side of a one-to-many relationship.

  • EntitySet<TEntity> - Entity class that provides for deferred loading and relationship maintenance for the "many" side of one-to-many and many-to-many relationships.

  • LookupList<T> - Entity class that represents the values of a lookup field (column) that allows multiple values.

For information about other important classes that can be used with the LINQ to SharePoint provider, see Microsoft.SharePoint.Linq. For overviews and programming tasks that describe the provider, see Managing Data with LINQ to SharePoint.

XML Used for SPMetal

SPMetal Parameters XML Schema is used to override certain aspects of the default behavior of SPMetal, determining in particular which content database entities are included in the entity classes that the tool generates. For more information about how the tool and its schema are used, see SPMetal.

Building Block: Lists and Document Libraries

More Information about Queries and Views

Queries and Data Retrieval in SharePoint Foundation

List Views