Hello, everyone in this tutorial we will see on how to create SAP OData service for ABAP CDS views using annotation @OData.publish: true. Let’s get started
We usually create OData services in the SAP Gateway system using transaction SAP Gateway Service Builder(SEGW).
Table of contents
ABAP CDS View OData Annotation
With the introduction of ABAP CDS views in SAP Business Suite 4(S/4 HANA), several thousands of CDS views are created for efficient and faster data access.
A new and easy way of creating OData services based on CDS views was introduced using the annotation @OData.publish: true at the view level.
Step-by-Step Procedure
Lets see how we can expose the ABAP CDS View as OData Service using an example and below is the exposure process. Here we will explain both scenarios CDS view with-out parameters and with parameters.
ABAP CDS View with out Parameters
1. Create an ABAP CDS view using ABAP Development Tools in Eclipse. Copy and paste the below code in the DDL editor.
@AbapCatalog.sqlViewName: 'ZV_ODATA_DEMO'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'OData service on ABAP CDS View'
define view ZODATA_DEMO as select from scarr
{
key carrid as AirlineCode,
carrname as AirlineName,
currcode as Currency,
url as AirlineURL
}
2. Add the annotation @OData.publish: true
above the DEFINE VIEW statement.
3. After adding the annotation to CDS View save and activate the view.
@AbapCatalog.sqlViewName: 'ZV_ODATA_DEMO'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'OData service on ABAP CDS View'
@OData.publish: true
define view ZODATA_DEMO as select from scarr
{
key carrid as AirlineCode,
carrname as AirlineName,
currcode as Currency,
url as AirlineURL
}
CDS view should meet the following rules for successful OData service generation.
- No syntax errors in DDL source code.
- At least one key element is defined in the SELECT list of the CDS view.
- The name of the CDS view should not exceed 26 characters in length.
4. After activating the CDS view, the following Gateway artifacts will be generated by the SADL framework in the back-end server.
- Technical service name artifact – <cds_view_name>_CDS.
- Gateway Model artifact with name – <cds_view_name>_CDS.
- ABAP class with name – CL_<cds_view_name>.
You can find all these artifacts in the transaction code – “/IWBEP/REG_SERVICE”.
5. Launch the transaction code “/IWFND/MAINT_SERVICE” to activate the OData service in the hub system. Enter System Alias, Technical Service Name, and click the Get Services button. The service will be displayed for selection.
6. In the Add Service dialog box enter the package name. Technical service name and Technical model name will already be suggested. Click on OK to continue.
7. A dialog box appears as shown below, click on OK to continue.
8. As a result OData service is activated successfully in the Gateway hub system. Launch the transaction code “/IWFND/GW_CLIENT” to test the service.
Above is the metadata generated for the OData service created based on the ABAP CDS view. Lets closely look at the metadata and identify the different artifacts as part of the OData service.
- Entity Type – ZODATA_DEMOType
- Entity Set – ZODATA_DEMO
Now lets see the data by accessing the entity set
http://<server>:8001/sap/opu/odata/sap/ZODATA_DEMO_CDS/ZODATA_DEMO
ABAP CDS View with Parameters
1. Below is the code for ABAP CDS view with parameters
@AbapCatalog.sqlViewName: 'ZV_ODATA_DEMO_P'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'OData service on ABAP CDS View'
@OData.publish: true
define view ZODATA_DEMO_PARAMS
with parameters p_carrid :S_CARR_ID
as select from scarr
{
key carrid as AirlineCode,
carrname as AirlineName,
currcode as Currency,
url as AirlineURL
}
where carrid = $parameters.p_carrid
NOTE: As of now there is no annotation/a way to make the input parameters OPTIONAL in ABAP CDS Views. So you have to pass values for all input parameters in ABAP CDS view while accessing the data from it.
2. Repeat the steps explained above in the Example #1 to create the OData service for this CDS view also.
3. On successfully OData service generation, execute the OData service and look at the metadata generated and following are the 2 entity sets generated
ZODATA_DEMO_PARAMSSet | Entity set which has 2 key predicates, one is the input parameter and the second one is the key of ABAP CDS View. You need to pass these 2 key predicates while accessing the data |
ZODATA_DEMO_PARAMS | Entity set which has 1 key predicate, which is input parameter. You need to pass this key predicate to access the data. |
To access the data you can use both entity sets and lets look how we can form the URI for both of them.
If you like to use entity set ZODATA_DEMO_PARAMSSet following the URI to be used
/sap/opu/odata/sap/ZODATA_DEMO_PARAMS_CDS/ZODATA_DEMO_PARAMSSet(p_carrid='AA',AirlineCode='AA')
If you like to use entity set ZODATA_DEMO_PARAMS following the URI to be used
/sap/opu/odata/sap/ZODATA_DEMO_PARAMS_CDS/ZODATA_DEMO_PARAMS(p_carrid='AA')/Set
Conclusion
Congrats!! You have successfully created an OData service for an ABAP CDS view. Please subscribe and stay tuned for more tutorials.
Please feel free to comment and let us know your feedback. If you liked it, please share it! Thanks!