Query SharePoint Foundation with ADO.NET Data Services
Last modified: June 17, 2010
Applies to: SharePoint Foundation 2010
In this article
Pros and Cons of Using the ADO.NET Data ServicesFramework to Query from a Client
Supported Types of Client Applications
Install ADO.NET Data Services on Your Development Server
Programming ADO.NET Data Services with a SharePoint Data Source
This topic provides a concise overview of how to query list data in Microsoft SharePoint Foundation by using ADO.NET Data Services Framework, a Representational State Transfer (REST) Web service. This topic presupposes a basic familiarity with this framework. For information about how to program with it, see Overview: ADO.NET Data Services and Using Microsoft ADO.NET Data Services.
You can also add, modify, and delete data by using the framework.
The principal advantage of using ADO.NET Data Services Framework to query SharePoint Foundation data from a client application is that query list data is strongly typed. In the SharePoint Foundation client object model, any two ListItem objects are the same type, even if they represent items from dramatically different kinds of lists. But ADO.NET Data Services uses an object-relational mapping that creates a type for each list in a target data context (Web site). For example, an item from an Announcements list is of a type called AnnouncementsItem and an item from a Tasks list is of a type called TasksItem.
The principal disadvantage of using ADO.NET Data Services Framework to query SharePoint Foundation data from a client application is that you can retrieve only list data and write to lists. Other kinds of SharePoint data are not accessible with ADO.NET Data Services.
If ADO.NET Data Services is not already installed on your development server, download the version appropriate for the OS:
For Windows Server 2008 R2 or Windows 7: ADO.NET Data Services Update for .NET Framework 3.5 SP1 for Windows 7 and Windows Server 2008 R2
There is a version of this service that is named "WCF Data Services" and that targets Microsoft .NET Framework 4. SharePoint targets Microsoft .NET Framework 3.5, so the "WCF" version should not be used.
Programming with ADO.NET Data Services is essentially the same regardless of the data source. In your Microsoft Visual Studio 2010 project, you add a RESTful service by specifying its URL. For SharePoint Foundation, the URL is always http://server/site_path/_vti_bin/listdata.svc, where site_path is the path to the Web site whose list data the client will be accessing. You are prompted to specify a name of the service reference.
When you add the reference, ADO.NET Data Services creates an object-relational mapping to the lists in the Web site and stores it in a file called reference.cs. A class named site nameDataContext, where site name is the name of the Web site, is created that derives from the DataServiceContext class. It has properties for each list in the Web site. Each such property is of type DataServiceQuery<TElement> where the type parameter is the type of item that populates the list. These item types are themselves declared in reference.cs. Each is named list nameItem, where list name is the name of a list. An example is: AnnouncementsItem. Each such item type has a property for every field (column) in the list.
Everything in the reference.cs file is inside its own namespace. The namespace is constructed by concatenating the namespace of your project, such as the namespace specified in a program.cs file (or form.cs file for a Windows Forms application) and the name you gave the service reference. For example, if Contoso.SharePoint.Client is your project namespace and MySiteService is the name you gave the service reference, the namespace for the object-relational mapping is Contoso.SharePoint.Client.MySiteService.
You create your program logic in the same way that you would with any other ADO.NET Data Services data source. Begin by adding a using statement for the short name of the service reference namespace just inside the namespace brackets of the code file that will contain your logic such, as program.cs or form.cs. The following is an example.
The next major step is to get a reference to a data context object, which serves as the gateway to the objects in the object-relational mapping. In the following example, MyServer is the name of a development server and MySite is the name of a Web site, so MySiteDataContext is a class defined in the object-relational mapping."
MySiteDataContext msdc = new MySiteDataContext(new Uri("http://MyServer/MySite/_vti_bin/listdata.svc"));
You can then query any list in the Web site with LINQ syntax queries, such as in this example.
var excitingAnnouncements = from announcement in msdc.Announcements where announcement.Title.EndsWith("!") select announcement;
If your code makes multiple queries to the same list, consider reading the whole list into a local List<T> object first and using that object as the source for further queries. The following is an example.
List<AnnouncmentsItem> allAnnouncements = msdc.Announcements.ToList(); var excitingAnnouncements = from announcement in allAnnouncements where announcement.Title.EndsWith("!") select announcement;
You can save changes to data made in the client application to the SharePoint Foundation list with the SaveChanges() method. The following is an example.