If you are following our series on SAP ABAP on HANA then you would be familiar with CDS Views. If not, then please check our HANA ABAP Part IV where we introduced Core Data Services and Part V where we took a Deep Dive into CDS Views. Also, if you have been taking the advantage of our SAP Netweaver Gateway and OData Services Tutorial series, you would know that SEGW is the t-code to create OData Projects and eventually publish an OData Service.
But would you not be surprised if we say, you can create your OData Projects without going to SEGW transaction? Today, we would show you how you can expose CDS View as OData Services with just some Annotations (i.e SQL code).
We have substantially explored the CDS views with major functionalities. CDS provides another magical strength to the users to expose the views as OData services. There is a conventional way to create a service in SEGW importing the view you created.
This article presents a technique to expose a view as a gateway service just by maintaining a cool annotation. No need to create service through SEGW. Sounds amazing? Let’s see how we can achieve that.
For CDS views we have used Eclipse Luna.
OData version 2 has been used for gateway application.
Step – I :
Create a view with a left outer join between tables VBAP and MARA. We have considered VBAP as ‘soitem’ and MARA as ‘prod’. A left outer join between two will allow you to select any fields from these two tables. For simplicity, we took only the fields mentioned in the key.
Fig.1-Create First View
Step – II :
Create a second view with Association. Associations in CDS views are more like associations in Gateways. You create an association to conceptually join or associate one data source to a target data source on a condition provided. If data sources can be envisaged as Entities of OData service then associations are joining two entities conceptually.
Fig.2- Create view with Association and OData Annotation
Take special note of the Annotation at the 6th line: @OData.publish: true. This is the magic spell for our article today. 🙂
Step – III :
Now our view is ready. With the DDL view we should be able to see data from Header table VBAK, Item table VBAP and Product table MARA.
Fig.3- DDLS view
Fig.3- Data from the view
Step – IV :
Note, once you activate the view you will be able to see an icon beside the annotation (6th line) “@OData.Publish: true” which reads that you need to register the service through /IWFND/MAINT_SERVICE.
Fig.4- OData Exposure in View
Step – V :
Now, as instructed go to transaction /IWFND/MAINT_SERVICE in the gateway system to register the service created through CDS.
Fig.5- Find Service in /IWFND/MAINT_SERIVCE
Step – VI :
Once the service is found, click on the service to register and save it in the appropriate package. Note we have not used SEGW to create any service. This service got automatically generated due to OData Annotation maintained.
Fig.6- Register Service
Step – VII :
Now test your service through /IWFND/GW_CLIENT transaction using proper OData query. Note, for navigation unlike usual gateway, we are using ‘to_<association name>’ in the query to navigate to the second data set. Since we created vbeln as an association condition in our ‘ZTEST_ASSOC_VIEW2’ the value needs to be passed through OData query for data fetching.
Fig.7- Test Gateway Data Fetch
Please also be informed that this service can only provide GET operation. No other CRUD operations can be done with this CDS view OData Exposure.
Usually, CDS views are created for fetching data (GET operations) and therefore even with the above limitation, this method of exposing CDS views as OData service is very helpful. This also shows the power of Annotations (New SQL) in the Core Data Services.
If you GENUINELY like our articles then it would be a HUGE help if you shared, subscribed and liked us on facebook. It might seem insignificant, but it helps more than you might think.
Step by Step Tutorials on S/4 HANA
- SAP S/4 HANA Technical Overview – Part I
- Data Access in S/4 HANA Cloud. Part 1 – CDS View Introduction
- SAP S/4 HANA Technical. Part 2 – Old & New Tables Confusion
Also, check HANA-ABAP Tutorials
- ABAP on SAP HANA. Part I. First Program in ABAP HANA
- ABAP on SAP HANA. Part II. ADT Eclipse and HANA Studio
- ABAP on SAP HANA. Part III. Debugging in ADT
- ABAP on SAP HANA. Part IV. Core Data Services
- ABAP on SAP HANA. Part V. Deep Dive into CDS Views
- ABAP on SAP HANA. Part VI. New Age Open SQL ABAP 740
- ABAP on SAP HANA. Part VII. SQL Script and SAP HANA Stored Procedure
- ABAP on SAP HANA. Part VIII. ADBC – ABAP DataBase Connectivity
- ABAP on SAP HANA. Part IX. AMDP – ABAP Managed Database Procedure
- ABAP on SAP HANA. Part X. AMDP with SELECT OPTIONS
- ABAP on SAP HANA. Part XI. Are Native SQL and Open SQL Competitors?
- ABAP on SAP HANA. Part XII. Open SQL, CDS or AMDP, which Code to Data Technique to use?
- ABAP on SAP HANA. Part XIII. Sample Functional Specification of HANA Project
- ABAP on SAP HANA: Part XIV. HANA Ready, HANA-tization & HANA Plus
- ABAP on SAP HANA: Part XV. Expose CDS Views as OData Service through Annotation
- ABAP on SAP HANA: Part XVI. HANAtization
- ABAP on SAP HANA: Part XVII. ATC – ABAP Test Cockpit Setup & Exemption Process
- SAP ABAP on HANA: Part XVIII. SALV IDA (Integrated Data Access)