Skip to main content

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.

  1. Create a new WinForms application and add a Data Grid (or any other DevExpress data-aware control) onto the form.
  2. 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.

    OData - Framework

  3. Install the OData Client for .NET NuGet package. To do so, invoke the Package Manager Console as shown below.

    EFCore - PMC

    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.

    OData V2 - Downgrade Package

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.

OData Connected Service - Visual Studio 2022 or newer (click to view)
  1. Download the OData Connected Service 2022+ extension from the Visual Studio Marketplace and double-click the .vsix file to install it.

  2. Right-click your project in Visual Studio and go to “Add | Connected Service”. Select “OData Connected Service” in the list. Add Connected Service to VS 22+ Project

  3. 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“).
    Configure Odata Connected Service Endpoint

  4. Choose required data tables in the “Schema Types Selection” tab.

  5. In the “Settings” tab, click “Advanced Settings” to open the complete list and check the option to ignore unknown elements. Configure Odata Connected Service Endpoint

  6. Press “Finish” to generate a client proxy.

OData Connected Service - Visual Studio 2017 - 2019 (click to view)
  1. Download the OData Connected Service extension from the Visual Studio Marketplace and double-click the .vsix file to install it.

  2. Right-click your project in Visual Studio and go to “Add | Connected Service”. Select “OData Connected Service” in the list.
    Add Connected Service to VS Project

  3. 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“).
    Configure Odata Connected Service Endpoint

  4. Choose required data tables in the “Schema Types Selection” tab.

  5. In the “Settings” tab, click “Advanced Settings” to open the complete list and check the option to ignore unknown elements. Configure Odata Connected Service Endpoint

  6. Press “Finish” to generate a client proxy.

OData Client Code Generator - Visual Studio 2017 or older (click to view)
  1. 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.
    OData - Install Code Generator
    This step is required only for your first OData v4 application. Once this VS template is installed, it is available for all future projects.

  2. 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.
    OData - New Data Source

  3. 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).

public static class Configuration
{
    public const string MetadataDocumentUri = "http://services.odata.org/V4/OData/OData.svc/";
    // . . .
}

Data Source Configuration Wizard

  1. Rebuild the project and invoke the Data Source Configuration Wizard. Select a Service and click “Next”.

    OData - Select Data Source

  2. Choose the required connection mode.

    OData - Select Binding 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.
  3. 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.

    OData - Last Wizard Page

    After you enter the URL, click the “Test Connection” button. If the connection was established successfully, the button becomes disabled.

    OData - Test Connection

  4. 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

  1. 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.

    OData v2 - New Source

  2. 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”.

    OData V2 - Select Service

  3. 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”.

    OData V2 - Select Data Source

  4. Choose the required connection mode.

    OData - Select Binding 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.
  5. 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.

    OData V2 - Final Wizard Page

    After you enter the URL, click the “Test Connection” button. If the connection was established successfully, the button becomes disabled.

  6. 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.