Silverlight: Basic Integration with SharePoint 2010 WCF Data Services
Posted by: Mahesh Sabnis
in Category Silverlight 2, 3, 4 and 5
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.
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:
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:
The above marked rectangle shows the SalesInfo List. Now I can query the SalesInfo List to display the entire data using the following URL
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:
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:
Step 3: Design the MainPage.xaml by dragging and dropping a DataGrid, button and a TextBlock on it:
Step 4: Include the following reference:
Step 5: Declare an object reference of the WCF Data Service Context as below:
Step 6: In the loaded event write the following code:
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:
Pease read the comments carefully which explaines the behavior of the code.
Step 8: Run the application and the result will be as below:
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