How To Publish CDS View as an OData Service?
OData is a web protocol, so it uses HTTP as its communication layer between different systems to perform query or manipulate data using RESTful APIs and it’s currently being used as the standard communication protocol between SAP and Non SAP systems.
What does OData have to do with CDS? Well, you can instantly publish a CDS View as an OData service using annotations. So today, we’ll demonstrate how to achieve this by leveraging CDS annotations to expose your CDS View as an OData service.
What you will learn? |
1. Using Annotation To Publish CDS as OData service |
2. Register and test the OData service using SAP Gateway |
3. Perform a query using filter and expand. |
Prerequisites |
1. SAP System Access: Developer role, SAP Gateway, Maintain OData service (/iwfnd/maint_service) |
2. Eclipse software |
CDS views have the capability to include metadata through annotations, which enhance their functionality and define behavior. These annotations act as rules and are evaluated dynamically at runtime, and the annotation that we’ll be using to publish the CDS view into an OData service is @OData.publish: true
Now follow these step by step instructions to create a CDS view and publish it using SAP Gateway
Step 1. Create The CDS View described in our previous tutorial. But because we want to demonstrate the query expand feature, modify the data source in the view to use the following SQL query as the data source:
@AbapCatalog.sqlViewName: 'ZCDS_SCARR_ODATA'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'scarr data'
@Metadata.ignorePropagatedAnnotations: true
@OData.publish: true
define view ZCDS_SCARR_DATA as select from scarr as header
association[1..*] to sflight as toItem
on header.carrid = toItem.carrid
{
key header.carrid,
header.carrname,
header.currcode,
toItem
}
As you can see, we added annotation @OData.publish: true in the metadata, so upon activation, it will create OData service based on the CDS entity name (ZCDS_SCARR_DATA).
Step 2. Login into the FIORI system and open the Service Catalog by using TCODE: /n/iwfnd/maint_service. Click the Add Service button.

Step 3. The OData service name will add prefix _CDS into the CDS entity name, so your OData service name will be ZCDS_SCARR_DATA_CDS as shown below.
Highlight / select the Service name and then click the Add Selected Service button.

Step 4. Next you can assign a package name for the OData service, but for this tutorial I’d just use the local object package.

After you click the Continue button, you will get the message confirming the activation.

Step 5. Next, click the Back button.

Step 6. Filter the service name from the list and you will see that ZCDS_SCARR_DATA_CDS service has been created and loaded successfully.

Now click the SAP Gateway Client button to launch the OData service.

Step 7. Click The EntitySets button and select the ZCDS_SCARR_DATA entity name.

Step 8. Click the Execute button to test the service. The OData will return the list of Carriers from the CDS View.

How To Filter The OData?
To filter the ODATA based on its keys, we need to add query option ?$filter as shown below.
/sap/opu/odata/sap/ZCDS_SCARR_DATA_CDS/ZCDS_SCARR_DATA?$filter=carrid eq 'AA'
This query option will filter the OData result based on the given keys. You can also filter the result based on several values as shown below.
/sap/opu/odata/sap/ZCDS_SCARR_DATA_CDS/ZCDS_SCARR_DATA?$filter=carrid eq 'AA' or carrid eq 'LH'

How To Expand The Details Data?
The data source for this CDS represents a master detail relationship, where each carrier is associated with its respective flight data. Initially, the result only returns a list of carriers. So, how can we retrieve the flight details for each carrier?
The solution is to use the $expand query option. However, to use this, you first need to identify the association name linking the header (carrier) to its details (flights). This association name can be found in the link tag within the metadata. In my case, the association name is totoItem.

So the expand query option will be ?$expand=totoItem&$format=json (I also added JSON format to display the result into JSON).
/sap/opu/odata/sap/ZCDS_SCARR_DATA_CDS/ZCDS_SCARR_DATA('AA')?$expand=totoItem&$format=json
Click the execute button and you will get this master and details result.

Summary
ABAP CDS Views provide a streamlined and efficient solution for exposing SAP data through OData services. By leveraging the power of CDS (Core Data Services), developers can define data models and business logic at the database layer, simplifying data consumption.
Once a CDS View is activated and configured with OData annotations (@OData.publish: true), it automatically generates an OData service endpoint, enabling seamless integration with external systems, applications, or UI frameworks. This approach reduces development effort and ensures consistency in how data is accessed and exposed across the SAP ecosystem.
References:
- Define relationship between entities using CDS Association
- OData Configuration Guide.