ODATA Reads
Data Reads
In this solution I have used Web API 2.2 with ODATA v4
ODATA v4 package
Visual Studio has a built in package to create ODATA v4 services, Package contains all the libraries required to create a ODATA v4 endpoint.
Integrate ODATA v4 package into solution
We can add the necessary OData package into solution like this, Select Microsoft ASP.NET Web API 2.2 for OData v4.0
All the relevant libraries are installing
Register OData endpoint
Register service endpoint in Register method of WebApiConfig file
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet("Students");
config.MapODataServiceRoute("ODataRoute", "odata", modelBuilder.GetEdmModel());
In line no 22, I have exposed the Student entity, Data read method is implemented in OData controller, Student Controller
ODATA Operations
Student Controller implements OData Controller
public class Students Controller : ODataController
{
List<Student> students = new List<Student>() { };
[EnableQuery]
public IQueryable<Student> Get()
{
return students.AsQueryable();
}
Student Controller inherits from OData Controller, So all the CRUD operations related to student can be implemented in this controller.
How it works in the browser
now ping to the OData endpoint,
We can see all the available entities in the endpoint with metadata.
Read Students
By appending entity, Student into url, we can read student data
Read Data types
We can read data types of student using the $metadata tag.
Excel with ODATA
Excel 2016 with OData v4
We already know office is not only for manipulating documents, We can perform many advanced queries with Excel. Excel supports to fetch data from different other sources, newer update is OData sources
In excel 2016, We can connect to a OData source like this,
But we get an error!!!!!!
It clearly says Excel 2016 can be only integrated with Odata v3 or any earlier data feeds :) But for your information, with Excel 2016 as well as Excel 2013, we can integrate OData v3
Excel 2013 & OData v4 ?
With Excel 2013, ODATA v4 is not compatible. But we can use a V3 format service with Excel 2013.
Excel 2016 with OData v4 - Power Query
With ODATA v4, we can’t use ODATA source option, But we have Power Query option to analyse data.
In Data tab, we have many options to fetch data from different other sources, We can select From OData Feed option.
We can use Office 2016 Power Query option with OData v4.
Power Query Editor
This is the query editor, we can select/remove columns, can remove duplicates, group by a column, create a new column based on a mathematical function and much more advanced operations.
In the next section, we’ll see how to read data by passing a value
Download
Tech Net Gallery
- source code can be downloaded from here,https://gallery.technet.microsoft.com/ODATA-Read-service-using-6b74f799
GitHub
- You can find it in my git hub repo, https://github.com/hansamaligamage/ODATAExample