SharePoint: Accessing SharePoint 2010 Lists by Using WCF Data Services



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 behttp://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.

C#
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 DataContextobject. 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 theDataContext object to call across the network and create a new item inside a SharePoint list.

C#
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.

2 thoughts on “SharePoint: Accessing SharePoint 2010 Lists by Using WCF Data Services

  • January 30, 2014 at 1:21 am
    Permalink

    I really appreciate if you can give a code for a button which will delete the SharePoint list items using same WCF application. I am able to retrieve the data from SharePoint list , now I want to delete the SharePoint list ALL items.

    Reply
  • April 10, 2014 at 3:32 pm
    Permalink

    var resultados2 in (from r in dc.VLISTAPERIODO orderby r.GHRIS select r));
    if (Convert.ToInt16(resultados2.PERIODO.ToString().Substring(0, 4)) >= DateTime.Now.Year && nperiodosTotal > 1)
    {
    dc.DeleteObject(resultados2);
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *