Export (0) Print
Expand All
Expand Minimize

Accessing SharePoint 2010 Lists by Using WCF Data Services

SharePoint 2010

SharePoint Visual How To

Summary:  Microsoft SharePoint 2010 introduces new REST-based web services by using Windows Communication Foundation (WCF) Data Services. By using the WCF entry point, a desktop application can reach across the network to query and update items in a SharePoint list.

Last modified: September 12, 2012

Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ted Pattison, Critical Path Training (Microsoft SharePoint MVP)

Overview

Although the LINQ to SharePoint provider introduces a new approach for writing code to access SharePoint list data, it can be used only by code that actually runs on the front-end web server. When your code is running from across the network (a desktop application, for example), you can achieve many of the same benefits of the LINQ to SharePoint provider by using new SharePoint 2010 support for REST-based web services that access SharePoint list items. This Visual How To shows you the essential details of how to get up and running.

Code It

In Microsoft Visual Studio 2010, create a project for a desktop application by using a project template such as WPF Application, Windows Forms Application, or Console Application. Next, you need a SharePoint site that contains lists that you want to access from your application. Inside the new project, create a service reference to ListData.svc by using the following steps.

To create a service reference to access SharePoint lists

  1. Open or create a Visual Studio project.

  2. In Solution Explorer, right-click the References node, and then click Add Service Reference.

  3. In the Address box, type the URL to the target site and append /_vti_bin/ListData.svc. For example, the address for the site intranet.wingtip.com would be http://intranet.wingtip.com/_vti_bin/ListData.svc.

  4. Change the default name in the Namespace box from ServiceReference1 to something more appropriate, such as WingtipSite.

  5. Click OK to create proxy classes, including a data context and entity classes for the lists that you want to access.

  6. Begin writing code against these proxy classes, which provide strongly typed access to the columns of SharePoint list items.

Programming with the DataContext Class

When you add a service reference to your project, the WCF Data Services support can inspect the target site and build a DataContext class that exposes a property for each list in the site. For example, if the target site has a list named "Developers", the DataContext object exposes a property named Developers that holds a collection of items that you can enumerate with a simple foreach loop. Be aware that you must set the proper credentials for the DataContext object before you make the first call to a SharePoint site.

WingtipDevSiteDataContext dc = 
  new WingtipDevSiteDataContext(new 
  Uri("http://intranet.wingtip.com/_vti_bin/ListData.svc/"));

dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

var source = dc.Developers;

lstDevelopers.Items.Clear();
foreach (var dev in source) {
    string devName = dev.FirstName + " " + dev.LastName;
    lstDevelopers.Items.Add(devName);
}

Adding New List Items

When you add a service reference to ListData.svc, in addition to generating a proxy class for the DataContext object, the WCF support also creates an entity class for each list. For example, if the target site has a list named "Developers", the creation of a service reference creates an entity class named DevelopersItem.

This entity class provides an easy way to add items to a SharePoint list. You use the entity class to create and initialize an object that holds the data for a new SharePoint list item. Next, you pass the object to one of the Add methods in the DataContext object. For example, if you have a list named "Developers", the DataContext object contains a method named AddToDevelopers. After you call the Add method with a new instance of the entity class, you can call the SaveChanges method on the DataContext object to call across the network and create a new item inside a SharePoint list.

WingtipDevSiteDataContext dc =
  new WingtipDevSiteDataContext(new 
  Uri("http://intranet.wingtip.com/_vti_bin/ListData.svc/"));

dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

dc.AddToDevelopers(
  new DevelopersItem {
    FirstName =  txtFirstName.Text,
    LastName = txtLastName.Text
  });

dc.SaveChanges();
}
Read It

By using WCF Data Services, your application can reach across the network and access data in SharePoint lists. Simply add a service reference to ListData.svc, and your code can use the DataContext class to gain strongly typed access to items in SharePoint lists.

See It

Watch the video

Watch video

Length: 00:12:30

Explore It

About the Author

Ted Pattison is an author, instructor, and co-founder of Critical Path Training, a company dedicated to education on SharePoint technologies. As a Microsoft SharePoint Most Valuable Professional (MVP), Ted frequently works with the Microsoft Developer Platform Evangelism group to research and author SharePoint training material for developers early in the product life cycle while in its alpha and beta stages. Ted also writes a developer-focused column for MSDN Magazine titled Office Space.

 

Show:
© 2014 Microsoft