Binding to OData
- 7 minutes to read
OData V4 is an open protocol initiated by Microsoft. Its RESTful API allows you to publish, read and edit resources defined in a data model using simple HTTP messages. The protocol currently includes three major versions: 2.0, 3.0 and 4.0. This document spotlights the most widely used versions: 4.0 and 2.0.
#Prerequisites
For both v4 and v2 OData sources, do the following.
- Create a new WinForms application and add a Data Grid (or any other DevExpress data-aware control) onto the form.
Make sure the target .NET Framework of your project is of version 4.5 or higher. To check and modify the target framework version, go to “Project | <Your_Project_Name> Properties”, then use the “Target framework” editor to select the required version.
Install the OData Client for .NET NuGet package. To do so, invoke the Package Manager Console as shown below.
Run the following command to install the package.
PM> Install-Package Microsoft.OData.Client
OData v4 and OData v2 use different versions of this package, so you may need to upgrade or downgrade it. Go to “Project | Manage NuGet Packages”, select the OData Client package, and change its version as shown below.
#Bind To OData V4 Services Using the Wizard
#Create a Client Proxy File
This step differs depending on the Visual Studio version you use. See this Microsoft help article for details: Client code gen tool.
Download the OData Connected Service 2022+ extension from the Visual Studio Marketplace and double-click the .vsix file to install it.
Right-click your project in Visual Studio and go to “Add | Connected Service”. Select “OData Connected Service” in the list.
Enter the data source URL in the “Endpoint” dialog tab. For testing purposes you can utilize any online sample service (for example, “http://services.odata.org/V4/TripPinService“).
Choose required data tables in the “Schema Types Selection” tab.
In the “Settings” tab, click “Advanced Settings” to open the complete list and check the option to ignore unknown elements.
Press “Finish” to generate a client proxy.
Download the OData Connected Service extension from the Visual Studio Marketplace and double-click the .vsix file to install it.
Right-click your project in Visual Studio and go to “Add | Connected Service”. Select “OData Connected Service” in the list.
Enter the data source URL in the “Endpoint” dialog tab. For testing purposes you can utilize any online sample service (for example, “http://services.odata.org/V4/TripPinService“).
Choose required data tables in the “Schema Types Selection” tab.
In the “Settings” tab, click “Advanced Settings” to open the complete list and check the option to ignore unknown elements.
Press “Finish” to generate a client proxy.
Right click your project in Visual Studio and select “Add | New Item…”, then switch to the “Online” tab. Find and install the “OData v4 Client Code Generator” item template.
This step is required only for your first OData v4 application. Once this VS template is installed, it is available for all future projects.Invoke the wizard and choose “OData V4 Services”. Click the “New Data Source…” button and select the “OData Client” option, then click “Add” to proceed.
After the new data source is ready, the .tt file with settings opens. In this file, locate the MetadataDocumentUri property and set it to a valid service document URI or a local file path. For testing purposes, use any online sample service (for example, “http://services.odata.org/V4/OData/OData.svc/“ or TripPin Service).
#Data Source Configuration Wizard
Rebuild the project and invoke the Data Source Configuration Wizard. Select a Service and click “Next”.
Choose the required connection mode.
- Direct Binding to Data Source - the data-aware control binds directly to the data source, without any go-between components involved.
- Binding via the BindingSource Component - a new System.Windows.Forms.BindingSource component is spawned to transfer data to your data-aware control.
- Asynchronous Server-Side Data Processing - data is transfered by the ODataInstantFeedbackSource component.
- Server-Side Data Processing - data is transfered by the ODataServerModeSource component.
The final Wizard page requires you to enter the data service root URL and a table from which to display data. For server and instant feedback modes, you can also choose a data field that serves as a key expression, and sort all data by another data field.
After you enter the URL, click the “Test Connection” button. If the connection was established successfully, the button becomes disabled.
- When all required fields are filled in, press “Finish”. Your data-aware control is now bound to Open Data.
#Bind To OData V2 Services Using the Wizard
Invoke the wizard and select “WCF Data Services”. Click the “New Data Source…” button to create a new source or select an existing one from the list.
In the “Add Service Reference” dialog that appears on screen, enter the OData service URL and click “Go” to establish a connection (try “http://services.odata.org/V2/Northwind/Northwind.svc/“ for testing purposes). In case of a successful connection, you will see all available services in the “Services” list. Expand a service to browse its data tables, then select the desired service and click “OK”.
Rebuild the project, then invoke the Data Source Configuration Wizard again. You will now see your service in the “Data Sources” list. Select it and click “Next”.
Choose the required connection mode.
- Direct Binding to Data Source - the data-aware control binds directly to the data source, without any go-between components involved.
- Binding via the BindingSource Component - a new System.Windows.Forms.BindingSource component is spawned to transfer data to your data-aware control.
- Asynchronous Server-Side Data Processing - data is transfered by the WcfInstantFeedbackSource component.
- Server-Side Data Processing - data is transfered by the WcfServerModeSource component.
The final Wizard page requires you to enter the data service root URL and a table from which to display data. For server and instant feedback modes, you can also choose a data field that serves as a key expression, and sort all data by another data field.
After you enter the URL, click the “Test Connection” button. If the connection was established successfully, the button becomes disabled.
- When all required fields are filled in, press “Finish”. Your data-aware control is now bound to Open Data.
#Bind To OData V4 Services In Code
The code below illustrates how to bind a Data Grid control to open data directly in code. The grid binds to an existing “DemoService” service, generated by the OData v4 Client Code Generator or OData Connected Service. For information on binding to Open Data in Server or Instant Feedback modes, refer to the ODataInstantFeedbackSource and ODataServerModeSource component descriptions.
public MainForm() {
InitializeComponent();
gridControl1.DataSource = new ODataDemoProject.DemoService(
new System.Uri("http://services.odata.org/V4/OData/OData.svc/")).Categories.ToList();
}
#Bind To OData V2 Services In Code
The code below illustrates how to bind to an existing “ServiceReference3” reference directly. For information on binding in Server or Instant Feedback modes, refer to the WcfInstantFeedbackSource and WcfServerModeSource component descriptions.
public MainForm() {
InitializeComponent();
gridControl1.DataSource = new MyApp.ServiceReference3.NorthwindEntities(
new System.Uri("http://services.odata.org/V2/Northwind/Northwind.svc/")).Categories.ToList();
}
#A 1-Click Solution for CRUD Web API Services with Role-based Access Control via EF Core & XPO
If you target .NET for your backend API, be sure to check out our free Web API Service and register your FREE copy today. The Solution Wizard scaffolds an OData v4 Web API Service (.NET 6+) with integrated authorization & CRUD operations powered by EF Core and our XPO ORM library. You can use OAuth2, JWT or custom authentication strategies alongside tools like Postman or Swagger (OpenAPI) for API testing.
Among its numerous capabilities, our built-in Web API Service filters out secured server data based on permissions granted to users. Advanced/enterprise functions include audit trail, endpoints to download reports, file attachments, check validation, obtain localized captions, etc.
To use the free Solution Wizard (which creates the Web API Service) run the Universal Component Installer from the DevExpress Download Manager.