SharePoint 2010 Data Services – Part 3

10 ביולי 2010

תגיות: ,
תגובה אחת

This is the third post of a series about SharePoint 2010 Data Services: Part 1, Part 2, Part 3 (this one).

In the previous post I talked about using SP 2010 Data Services via the browser, or passing the feed URL to some other component. This could be helpful for code-less mesh-ups, or client side programming. But there’s more to Data Services then just that, It is also a pretty good alternative to Client OM. In this post I will explore the programmability possibilities of Data Services from Visual Studio 2010.

We already know how to consume Data Services using HTTP requests. This can also be done from code, but it will require us to manually construct queries, HTTP requests, then execute them, and finally parsing the results. With Visual Studio 2010, you can consume Data Services without performing these manual operations yourself. Visual Studio can generate proxy classes for you, that provides an easy way to work with your data, as well as a type safe one.

First, from VS 2010, open the “Data” menu, and select “Add new Data Source…”. As you can see, there’s a new “SharePoint” Data Source.


Select it, and you are taken to the add new service reference dialog. By the way, this is the same as adding service reference directly from the server explorer, as shown below.


Type in the address of the service (“http://server/site/_vti_bin/listdata.svc”), and click “Go”. I chose also to change the service namespace at this point.


Finish the wizard, and you have added the data source. What you have done in this step is not connecting to the service, but discovering it, analyzing the site content, and generating proxy classes that can work against the site’s data.

Now it is time to establish a connection. First, create a new data context. This is an object named like your site’s name appended with the phrase “DataContext” (in my case it’s “ItayskDataContext”). You have now established a connection to the site. Since we are operating from the client, we need to specify credentials with which to authenticate.

ItayskDataContext service =
new ItayskDataContext(new Uri("http://dev-sp2010-01/sites/itaysk/_vti_bin/listdata.svc"));
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

Now lets get some data from the site. The beauty with Data Services, is that it lets you work with familiar tools such as object oriented approach and LINQ queries, and takes care of all the plumbing for you. Getting all items from an announcements list with the word “itay” in their title will look like this:

var res = from a in service.Announcements
where a.Title.Contains("itay")
select a;

A few things to notice here: First, we are querying an object that represents the list. This is the OO approach I was talking about (just like SPMETAL and LINQ2  SharePoint). The data context object has a property for each list in the site. Second, we are using LINQ to query the data source.

If you want to know what’s going on backstage (also useful for debugging), launch Fiddler, and we see this response\request:


That’s an HTTP GET request… (remember what we said about Data Services exposed as HTTP requests?) Now, what if we wanted to add a new item to the list? We obviously need to change the HTTP verb from GET to POST, but as we said, we don’t want to deal with plumbing. This is why the data context object has special methods like “AddToAnnouncements” which accepts an “AnnouncementsItem”.

Modifying an item is as easy as obtaining an item, and changing one of it’s properties. To delete an item, call the “DeleteObject” methods of the data context, and pass the item you wish to delete. Finally just call

the “SaveChanges()” method of the data context. This will create an execute requests with the appropriate headers.

Here is how it looks in code. This piece of code:

//Create an Announcements object
AnnouncementsItem temp = new AnnouncementsItem();
temp.Title = "Hi!!";
//Add new item
//Modify item
temp.Title = "Bye!!";
//Delete item

will result in these calls:


In this post we have learned how to use Data Services as a data source for your client application. Data Services are a very useful way to work with SharePoint that requires no special code on the client side, and hence fits almost every scenario.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

תגובה אחת

  1. Ashok Khurana22 בספטמבר 2011 ב 11:27

    When I write above code for my List and executes from a cansole application, it runs ok but nothing reflect in backend.