Silverlight: Basic Integration with SharePoint 2010 WCF Data Services

Posted by: Mahesh Sabnis , on 10/10/2011, in Category Silverlight 2, 3, 4 and 5
Views: 89183
Abstract: The advantage of WCF Data Services is that any client application can now make a call to the WCF services and interact with the SharePoint object model. It is not necessary for the end-user to directly log-in to the SharePoint Web Site application and work on it. WCF Data Services provided in SharePoint 2010 helps to implement integration based application with other client applications.
If you have a requirement of integrating SharePoint 2010 with various client applications built using WinForm, WPF and Silverlight etc. then you can make use of the various out-of-box programming facilities provided by SharePoint 2010. SharePoint 2010 offers Server Object Model and Client object model using which a developer can develop Desktop interfaces to interact with SharePoint and perform operations against List etc. objects. It is also possible for developers to create Custom WCF service which acts as an interface between desktop clients and SharePoint 2010.

Now beyond writing custom WCF services, you as a developer can very well think of using REST based WCF Data services provided in SharePoint 2010. You can find e.g. ListData.svc in ‘ISAPI’ folder. Using this service, you can implement Silverlight client application for working with SharePoint 2010 web site’s List data.

Here to interact with SharePoint 2010 from Silverlight client, run your application Out-of-Browser.

The WCF Data Service can be found in the following path:


Note: Here MyServer is the name of your SharePoint 2010 Application Host server. Now if you have the SPS 2010 application as Products, then you will find the ListData WCF service  in the following path:

 

Querying the List WCF Data Service


Shown below is a Products Web Collection with a SalesInfo List:
SalesInfo List
 
You can browse the WCF Data Service which will Display all Lists in the Web Site collection, using the following URL:

http://mahesh-pc/sites/products/_vti_bin/ListData.svc (change the url with your server name)

The result is as below:

WCF Data Service

The above marked rectangle shows the SalesInfo List. Now I can query the SalesInfo List to display the entire data using the following URL

http://mahesh-pc/sites/products/_vti_bin/ListData.svc/SalesInfo

ListData

The advantage of REST capability is that now any client application Desktop, Web, Browser which can communicate via HTTP and can understand XML, can make a request to the SharePoint 2010 object model (of course with proper Authentication and Authorization) and can interact with the data.
 

Consuming the SharePoint 2010 WCF List Data Service in Silverlight Client application


This is the main part of this article. The scenario is explained as under:

clip_image006[6]

Since the Silverlight application has to carry the credential information for the SharePoint Web Site collection, to make a call to the WCF Service exposed by SPS 2010, it needs to make use of trusted network credential facility. That’s the reason a Silverlight Out-of-browser client application is used.

Step 1: Open VS2010 and create a Silverlight application, name it as ‘SL4_Consuming_SPS_2010_RestService’. In this Silverlight application, add a Service Reference to the WCF List Data Service and name the namespace as ‘MyRef’.

Step 2: Right-Click on the Silverlight Project and from the properties, configure it to run in ‘Out-of-Browser’ mode with elevated rights as shown below:

Out Of Browser app

Step 3: Design the MainPage.xaml by dragging and dropping a DataGrid, button and a TextBlock on it:

XAML
image

Step 4: Include the following reference:

using System.Data.Services.Client;

Step 5: Declare an object reference of the WCF Data Service Context as below:

MyRef.ProductsSiteDataContext objDataContext;

Step 6: In the loaded event write the following code:

image

The above URL represents the repository of the List WCF Data Service in the Product site collection. The DataContext (ProductSiteDataContext) class defines classes for all Lists defined in the SPS2010 web site collection.

Step 7: In the click event of the ‘Get Sales Info’ write the following code:

image

Pease read the comments carefully which explaines the behavior of the code.

Step 8: Run the application and the result will be as below:

clip_image002[14]

Conclusion: The advantage of the WCF Data Service facility is that any client application can now make a call to the WCF services and interact with the SharePoint object model. It is not necessary for the end-user to directly log-in to the SharePoint Web Site application and work on it. WCF Data Services provided in SharePoint 2010 helps to implement integration based application with other client applications.

The entire source code of this article can be downloaded
over here
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles


Page copy protected against web site content infringement by Copyscape


User Feedback

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel