Data Access in SharePoint 2010

SharePoint 2010 introduces several new ways in which you can programmatically interact with your data. Most notably, the introduction of LINQ to SharePoint allows you to build complex list queries with the user-friendly language integrated query (LINQ) syntax instead of constructing queries with the somewhat cumbersome Collaborative Application Markup Language (CAML). Both LINQ to SharePoint and CAML now support join predicates in queries, which moves the SharePoint list-based data model a step closer to the power and flexibility of a relational database. One of the major evolutions in the latest version of SharePoint is the blurring of the distinction, from the user's perspective, between internal and external data. The introduction of external lists in SharePoint2010 means that you can use many of the same techniques to query data, regardless of whether that data resides in a SharePoint content database or an external system.

The introduction of new options for data access brings new challenges and best practices for developers. This chapter provides an overview of each approach to server-side data access and provides guidance on the benefits and potential pitfalls of each approach.

Note

Client data access techniques, such as the new client object model and REST services, are not covered in this chapter. These techniques cover a large area of functionality and merit a chapter of their own. For more information, see Data Access for Client Applications.

SharePoint 2010 offers three key approaches that you can use to query data:

  • CAML queries (SPQuery and SPSiteDataQuery). The SPQuery and SPSiteDataQuery classes allow you to construct and submit CAML queries to perform data operations. CAML suffers from a number of shortcomings, including quirky syntax, lack of tooling support, and difficult debugging. However, CAML remains the core engine for data operations and is still the most appropriate choice in some scenarios. The CAML schema has been expanded in SharePoint 2010 to include support for join predicates. You can also use the SPQuery class to query external lists. Note that the results returned by CAML queries are non-typed items.
  • LINQ to SharePoint. SharePoint 2010 allows you to use LINQ queries to perform data operations on SharePoint lists, including support for join predicates. LINQ to SharePoint operates against strongly-typed entities that represent the items in your lists, and SharePoint 2010 includes a command-line tool named SPMetal that you can use to generate these entities. Internally, the LINQ to SharePoint provider generates the underlying CAML queries that perform the data operations.
  • The Business Connectivity Services (BCS) object model. SharePoint 2010 includes new BCS APIs that enable you to interact with external data. The BDC Object Model allows you to programmatically navigate the data entities and associations in your Business Data Connectivity (BDC) models and to invoke the stereotyped operations defined on these entities.

The following table provides a summary of the different scenarios in which you can use each of these approaches to data access.

Scenario

LINQ to SharePoint

SPQuery

SPSiteDataQuery

BDC object model

Querying SharePoint list data within a site

Ff798432.2ff33ea5-e2e3-4525-a0bb-43baf869f0ec(en-us,PandP.10).png

Ff798432.3de76a5a-ab8f-47e9-9563-9cfdb04fc9fd(en-us,PandP.10).png



Using joins on SharePoint lists within a site

Ff798432.9034144f-ddb4-4b81-9801-9e26707f15d0(en-us,PandP.10).png

Ff798432.d3d5475e-fd73-4f58-9933-280952601424(en-us,PandP.10).png



Using joins on SharePoint list data across sites within a site collection

Ff798432.c38cba40-b92b-456f-9ca9-af70513ea1b6(en-us,PandP.10).png

Ff798432.488f8ff2-8515-49d1-9292-1b3e32d86eee(en-us,PandP.10).png



Aggregating list data across multiple sites within a site collection



Ff798432.5ee3e850-04c2-486e-87d2-662be23dd92f(en-us,PandP.10).png


Querying external list data within a site


Ff798432.b61f098e-062c-451a-a6c1-28db655080e6(en-us,PandP.10).png


Ff798432.df8c73dd-688f-473a-bd58-e6e81cd1e6ba(en-us,PandP.10).png

Navigating associations between entities in a BDC model (external data)




Ff798432.4ab0ad97-fe25-4573-827a-ae5e53fad013(en-us,PandP.10).png

Accessing binary streams from external data




Ff798432.5b5b5bab-9144-4f66-beb5-68b4b738b154(en-us,PandP.10).png

Accessing external data from a sandboxed application (requires the use of external lists)


Ff798432.ea86a406-c6ee-470a-b55a-5b82ac5aaa33(en-us,PandP.10).png



Querying external data that returns complex types




Ff798432.65626da0-772c-4f9f-be97-66e32e62e941(en-us,PandP.10).png

Querying external data that uses a non-integer, or 64-bit integer, ID field




Ff798432.fbd3eb25-8713-48a6-be7d-5d8852a33144(en-us,PandP.10).png

Navigating bidirectional associations between entities in a BDC model (external data)




Ff798432.395fd5ff-3e0f-4460-bf1f-51985cf8cca7(en-us,PandP.10).png

Locating an entity by specifying a field other than the ID field (external data)




Ff798432.5fa7cd86-1f03-45f2-8774-2708f7c7d4fe(en-us,PandP.10).png

Querying entities that include fields that do not map to an SPFieldType (external data)




Ff798432.b1a42522-999e-43fc-baa1-1e68144b0c96(en-us,PandP.10).png

Perform bulk operations on external data




Ff798432.84bc3fd2-5595-4464-b33f-0c682b36c30a(en-us,PandP.10).png

Note

For information about how to use LINQ to SharePoint and CAML queries, see Querying from Server-side Code on MSDN.

This section includes the following topics that will help you to understand the key details, performance issues, and best practices behind the different approaches to data access in SharePoint 2010:

  • Using Query Classes. This topic identifies scenarios in which you should consider using the CAML-based query classes—SPQuery and SPSiteDataQuery—for data access.
  • Using LINQ to SharePoint. This topic examines the use of the new LINQ to SharePoint provider, and identifies key efficiency issues and potential stumbling blocks.
  • Using the BDC Object Model. This topic identifies scenarios in which you must use the BDC object model for data access and provides insight about how you can use the BDC object model to accomplish various common tasks.

Out of Scope Topics

There are a number of additional approaches to data access that are beyond the scope of this documentation. You may want to consider the following components for more specialized scenarios:

  • The ContentIterator class. This class is new in SharePoint Server 2010. It enables you to iterate sites, lists, and list items in chunks, in order to avoid violating query throttling thresholds. This class is useful in circumstances where you cannot avoid iterating large lists. If you think a list is likely to grow large, consider using the ContentIterator class to access the list from the start. For more information, see Large Lists.
  • The ContentByQueryWebPart class. This class is part of the publishing infrastructure in SharePoint Server 2010. It allows you to submit a CAML query across sites and lists within a site collection and then format the HTML output by specifying an XSL transform. The ContentByQueryWebPart class makes extensive use of caching to provide an efficient means of data retrieval.
  • The PortalSiteMapProvider class. This class is also part of the publishing infrastructure in SharePoint 2010. It provides an efficient mechanism that you can use to query and access cached objects within a site collection. The PortalSiteMapProvider class offers a useful alternative to the ContentByQueryWebPart class when you require programmatic access to the objects you are retrieving and is capable of caching query results from SPQuery or SPSiteDataQuery. Like the ContentByQueryWebPart class, the PortalSiteMapProvider makes extensive use of caching for reasons of performance and efficiency.