CDS Part 3. Expose CDS Views as OData Service through Annotation

Introduction

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.

Technical Environment

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_’ 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

Limitations

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.