The Open Data Protocol, also known as OData recently has gained a lot of popularity. That is why we decided to demonstrate how to connect and consume an existing OData feed from Telerik Reporting with the help of WCF Data Services. For the sake of this example we will connect to the Northwind sample database exposed as an OData service with the following root URL:
http://services.odata.org/Northwind/Northwind.svc.
Let's start by adding a service reference to the Northwind data service to our report class library - we use the above service URL for this purpose:
Visual Studio creates a reference to the service in your project and generates automatically a DataServiceContext and an entity model for accessing the exposed data. The DataServiceContext class is called NorthwindEntities by default and defines properties for querying the entities from the Northwind database.
Next we define a query to retrieve some data for the report - for the sake of this example, we should retrieve information about the products by category. To do this add a new class NorthwindDataAccess to the project and define the following method:
C#
public
class
NorthwindDataAccess
{
public
IEnumerable<Products_by_Category> GetProductsByCategory(
string
categoryName)
{
var serviceRoot =
new
Uri(
"http://services.odata.org/Northwind/Northwind.svc"
);
var serviceContext =
new
NorthwindEntities(serviceRoot);
var productQuery = from product
in
serviceContext.Products_by_Categories
where product.CategoryName.StartsWith(categoryName)
select product;
return
productQuery.ToArray();
}
}
VB.NET
Public
Class
NorthwindDataAccess
Function
GetProductsByCategory(
ByVal
categoryName
As
String
)
As
IEnumerable(Of Products_by_Category)
Dim
serviceRoot =
New
Uri(
"http://services.odata.org/Northwind/Northwind.svc"
)
Dim
serviceContext =
New
NorthwindEntities(serviceRoot)
Dim
productQuery = From product
In
serviceContext.Products_by_Categories _
Where product.CategoryName.StartsWith(categoryName) _
Select
product
Return
productQuery.ToArray()
End
Function
End
Class
Finally create a new report and use the ObjectDataSource component to bind to the NorthwindDataAccess.GetProductsByCategory method as a data source for the report.
As you can see, connecting a Telerik Report to OData feed is straightforward and executed in a seamless manner using the powerful Telerik Reporting's data source components without breaking the best architecture patterns in a few simple steps. The video below shows these short steps in action.
The video is also available in Telerik TV: Connecting Telerik Reports to OData Feeds.
For more information about the OData protocol and WCF Data Services you can visit the following useful links:
Stefan’s main interests outside the .NET domain include rock music, playing the guitar and swimming.