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.
Also, check our popular step by step tutorials on some of the important topics of SAP ABAP.
1. ABAP for SAP HANA Tutorials
2. ABAP Web Dynpro Tutorials
3. GOS Tutorial
4. OOPs ABAP Tutorial
5. HANA Tutorial
6. SAP Netweaver and OData Tutorial
7. SAP Adobe Form Tutorial
8. SAP Fiori Tutorial
9. SAPUI5 Tutorial
10. SAP Screen Personas Tutorials