Using Data Services over SharePoint 2010 – Part 1 – Getting Started

Published 21 October 09 11:21 AM | dpblogs 

ADO.NET Data Services 1.5 and SharePoint 2010 allow developers to write applications against SharePoint Lists using the familiar Data Services Client programming model.

Setup

The SharePoint 2010 beta will become generally available in November.

In order to use Data Services with SharePoint 2010 Beta, ADO.NET Data Services 1.5 must be installed on your server, ideally *before* you install SharePoint 2010.

If however SharePoint 2010 is already on your box when you install Data Services, you will need an iisreset to make the Data Service Endpoint show-up.

And then to program against a Data Services enabled SharePoint box you need either:

  • VS 2008 SP1 with the ADO.NET Data Services 1.5 installed (CTP 2 or higher)

    or
  • VS 2010 (Beta 2 or higher)

Where is your Data Service Endpoint?

Assuming you’ve got everything setup and working the first thing you need to know is where your Data Service endpoint is installed.

If your SharePoint site is http://mflasko-dev/ (thanks Mike) then your Data Services endpoint will be http://mflasko-dev/_vti_bin/listdata.svc.

If you open one of these endpoints up in Internet Explorer you’ll see something like this:

DataServiceRoot

As you can see this is a standard Data Services Atom feed, listing all the Lists on the SharePoint site.

Cool.

Once you know where your Data Service is located, the next step is to write your Data Services Client application.

Create the Sharepoint Application:

But first to demonstrate how easy this is, I’ve used SharePoint to create the simplest of all Suggestion Tracking applications.

You can follow along by following these simple steps:

  1. Create a ‘Suggestions’ list which is based on an ‘Issues’ list.
  2. Finished!

The resulting list looks something like this:

Suggestions 

The idea is that this list will capture all the suggestions users have for making a departmental line of business (LOB) application better.

But rather than making people go to the SharePoint site to make the suggestion, we want to allow the users to do it right in the LOB application.

Talking to Sharepoint using Data Services:

The first step as always to add a service reference, using the location of the Data Service that exposes SharePoint data (i.e. http://mflasko-dev/_vti_bin/listdata.svc)

AddServiceReference

Once you’ve done this you will have CLR classes representing the types in each list, and you will have a strongly typed DataServiceContext called TeamSiteDataContext that allows you to access the data:

ObjectBrowser

Rather than focusing on how the user enters the suggest in your LOB application, lets focus on how to POST that suggestion to Sharepoint.

To do that you need to write a method something like this:

public void PostSuggestion(string title, string description)
{
   TeamSiteDataContext ctx = new TeamSiteDataContext(
        new Uri(
           
http://mflasko-dev/_vti_bin/listdata.svc
            UriKind.Absolute
        )
   );
   ctx.AddToSuggestions(
      new SuggestionsItem{
          Title = title, 
          Description = description
      }
   );
   ctx.SaveChanges();
}

As you can see this is very easy.

In an environment like Silverlight it is a little more complicated because whenever you cross a network boundary have to deal with the Async and Threading issues. Which means you can’t use SaveChanges() directly.

You have to use the standard BeginXXX() and EndXXX() async pattern and you need to use the Dispatcher to ensure the results are marshaled back on the UI thread:

ctx.BeginSaveChanges(
      (IAsyncResult result) => Dispatcher.BeginInvoke(
          () => ctx.EndSaveChanges(result)),
       ctx
   );

In future blog posts we will dig into this in more detail, covering topic like Query / Update / Data Binding, Blob Handling and security policies over Sharepoint.

As always we are keen to hear your comments.

Alex James
Program Manager
Microsoft

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# BobS said on October 21, 2009 11:42 AM:

Are you sure that Sharepoint 2010 beta is available now on MSDN?

# dpblogs said on October 21, 2009 1:28 PM:

BobS,

I made a mistake sorry. I just removed that comment.

Sorry for the inconvenience.

Alex

# Nick said on October 22, 2009 9:47 PM:

Does this work against the current (private) SharePoint beta?

# Joerg said on October 25, 2009 9:22 AM:

Hi,

Hi tried with SharePoint 2010 TAP+VS 2010 Beta2 and get a TypeLoadException:

Could not load type 'System.Data.Services.OpenTypes.LateBoundMethods' from assembly 'Microsoft.Data.Services, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Using Reflector I see that OpenTypes.LateBoundMethods gets called in the link provider but is not available in the Microsoft.Data.Services assembly. Even Reflector cannot resolve and SharePoint failes too.

Against what ADO.NET DS Version is SharePoint 2010 built and will the Beta coming in November will work against DS 1.5 CTP 2 (or whatever)?

What can I do now to work with this to check out before next release?

Thanks,

Joerg

Running Win2008x64+SP2010+VS2010!

# Joerg said on October 25, 2009 9:25 AM:

Hi,

btw, using http://<myserver>/_vti_bin/listdata.svc returns with an error showing the same TypeLoadException (cannot load assembly...the one already in the GAC - double checked it's right version/token but seems to call wrong types).

Best,

Joerg

# Alberto Diaz said on January 28, 2010 1:12 PM:

Hello,

I have a answer about this article. I tried to insert a new task on a Tasks List and I found that I need to put a value on Created and Modified column. But this columns aren't required fields, this columns are system fields.

Why I had to write this value and cannot let it blank?

If I let it blank, I had an exception telling that an error was encountered when parsing a DateTime value in the XML

Thanks,

Alberto Diaz

adiazcan@hotmail.com

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker