Partager via


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

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:

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 https://mflasko-dev/ (thanks Mike) then your Data Services endpoint will be https://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. https://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(
            https://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

Comments

  • Anonymous
    October 21, 2009
    Are you sure that Sharepoint 2010 beta is available now on MSDN?

  • Anonymous
    October 21, 2009
    BobS,I made a mistake sorry. I just removed that comment.Sorry for the inconvenience.Alex

  • Anonymous
    October 22, 2009
    Does this work against the current (private) SharePoint beta?

  • Anonymous
    October 25, 2009
    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,JoergRunning Win2008x64+SP2010+VS2010!

  • Anonymous
    October 25, 2009
    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

  • Anonymous
    January 28, 2010
    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 XMLThanks,Alberto Diazadiazcan@hotmail.com

  • Anonymous
    March 21, 2010
    In the future, will other web services be exposed as well? Will we be able to manage users, get a simple of users, etc?

  • Anonymous
    December 13, 2010
    Hi,While adding the site as service reference(http://myserver/site/_vti_bin/listdata.svc) to my application,i got an exception. I have tried to connect this using Linqpad 4, Now also i' am getting exception. The exception is due to _1HistoryItem in this site. But i dont have any list or list items with this name. I found solution for this from one blog to load the csdl file in visual studio and to do something . But i was not able to succeed. Please help me on this.Thanks,varathi.

  • Anonymous
    February 07, 2011
    Hi,I'm using vs2010 professional..I need to know how to demonstrate about OData in vs2010.Regards,Anandhan

  • Anonymous
    February 23, 2011
    We have just finished deploying SharePoint 2010 in our company, when i try to access a data service endpoint it simply times out (either through code or browser) the endpoint i am trying to his is http://SharePointServer/DmsViewer/_vti_bin/listdata.svc/ProductionLinks() if i then browse the list and retry the link it will then work but it seems that i need to browse the list with IE first (and maybe spin up the site) is there a way to make a data service call access a site without having to browse it first with IE?

  • Anonymous
    November 26, 2012
    Is it possible to change the site at runtime.  Since the service reference datacontext is the one for the site defined during development, you must develop using production only.  We are not permitted to develop using production data.  We need to develop against test data, move the app to production and change the url at runtime to point to the production list.  Can't really do this with SharePoint services since proxy's are not provided.