SharePoint Pals
 | Sign In
Create WCF Data Service and expose the data using OData protocol
by Tarun Kumar Chatterjee 27 Jun 2016
.Net – Technology Specialist
Today  :   6     Total  :    1969

WCF Data Service can expose data from any source by using data provider and service uses the OData protocol for communication. Response will return in either Atom or JSON format.

Following set of operation can be done by applying query in url

1. To return the resource in different format

2. Filter the records

3. Read only the required fields

4. Pagination

5. Ordering the records

In this article let's understand the details implementation on WCF Data Service and OData

Create an empty web application project by selecting "ASP.NET Empty Web Application".


Right click the project and select "Add"->"New Item" and select "ADO.Net Entity Data Model"


Entity Data Model Wizard will appear, select "Generate from Database" and click "Next"

In the Database connection click the "New Connection" button and specify the server name and select "EmployeeDB" database and click "OK"

Click on Next to create Entity Data Model for the "Employee" table and it is exposed as property with name "Employees".

Below is the code of ModelTest.Context.cs

 namespace WCFODataService
     using System;
     using System.Data.Entity;
     using System.Data.Entity.Infrastructure;
     public partial class EmployeeDBEntities : DbContext
         public EmployeeDBEntities()
             : base("name=EmployeeDBEntities")
         protected override void OnModelCreating(DbModelBuilder modelBuilder)
             throw new UnintentionalCodeFirstException();
         public DbSet<Employee> Employees { get; set; }

Right click the project select "Add"->"New Item" and select "WCF Data Service"


Below example, I have set the "Read" access to the "Employee" property of the data context. So collection of "Employee" can be read by any one by applying query expression.

 namespace WCFODataService
     public class WcfDataServiceTest : DataService<EmployeeDBEntities>
         public static void InitializeService(DataServiceConfiguration config)
             config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);           
             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;

Run the service and you will see the output as shown below.


Enter the URL as mention below to return all the "Employees" resources in form of Atom: http://localhost:50627/WcfDataServiceTest.svc/Employees


Return the Employees in Json format


To filter the Employees by EmployeeId = 2


Return top 2 employees sorted with EmployeeId Desc


WCF Data Service, enables the capability of exposing the entity objects as service objects and also the service methods can be invoked directly from the request url. The method of invoking the service method(s) from url is same as like in MVC invoking through url or same as like REST API.

Beside the possible way of invoking from browser, the same url can be treated as WCF Service and can be used to generate a proxy class in the client application by adding through the service reference.

 namespace WCFODataService
     public class WcfDataServiceTest : DataService<EmployeeDBEntities>
         public static void InitializeService(DataServiceConfiguration config)
             config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
             config.SetServiceOperationAccessRule("GetAllEmployeeDetails", ServiceOperationRights.AllRead);
             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
         public IQueryable<Employee> GetAllEmployeeDetails(string filter)
             return (new List<Employee>()
                         new Employee() { EmployeeId = 1, DepartmentId = 1, Name  = "Tarun1", CreatedDate=new DateTime(2010,7, 21), Salary=200000},
                         new Employee() { EmployeeId = 2, DepartmentId = 2, Name  = "Tarun2", CreatedDate=new DateTime(2010,7, 22), Salary=100000}

Build and run the service now, URL: http://localhost:50627/WcfDataServiceTest.svc/GetAllEmployeeDetails?filter=’All


Hope this artifact helps you to have some basic idea on OData service and its utilities.

Happy Coding

Tarun Kumar Chatterjee

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation