CDS (Core Data Services) is one of the newer SAP technologies. Similar to ABAP Managed Database Procedures (AMDP), it can be used to increase performance by enabling data models to be defined and used at the database level instead of the application level.
CDS Views are virtual data models that can be used to access Hana tables. You can use standard CDS Views or create custom ones. It is also possible to extend an existing view with additional fields rather than create a new one. CDS has a wide range of uses, below I will explain one example use of how to extend a basic CDS view and expose it as an OData service.
1. CDS View Extension
Firstly, launch the Eclipse ABAP Development Tools, search for ‘Data Definition’ and then click ‘Next’. After that, we will create the first view. It will be a basic view so choose the ‘Define View’ template and click ‘Finish’.
Congratulations the first view is generated!
In the second step, add the DB table and its fields to get the data, and then run the program to see the result.
As clearly seen, your view contains only the fields you added and the data we chose with the ‘where’ condition.
In the third step, we will create another view to extend your basic view. By following the same steps we will create a new data definition but this time the template chosen should be ‘Extend View’.
Congratulations, your second view is also generated!
There is only one step left, instead of ‘view_name’ define the name of the view you want to extend and the fields to be used in the extension.
Now, this part is also done! After activating this program, you can turn back to our basic view and run to see the difference.
Before the extension, your custom view had five fields and now it has three additional fields.
2. Expose CDS Views as OData Services
In the first part of this blog, we explained CDS view extension and now we’ll show how you can create an OData service using your view. There are actually three ways to create OData services with CDS Views, here we prefer to choose to use @Odata.publish annotation since it does not require transaction SEGW (which is a more traditional way).
First, add ‘@Odata.publish: true’ annotation to your basic view.
Then launch SAP GUI, go to /n/IWFND/MAINT_SERVICE transaction and click ‘Add Service’.
You will then be redirected to a new page. We want to find your backend service here, and to do that write ‘local’ to System Alias and the name of our CDS View into the Technical Service Name parameter.
Once we have found our backend service, click on it and make the package assignments, then continue.
Now, your service is also ready!
You can turn back to /n/IWFND/MAINT_SERVICE transaction and find our service using ‘Filter’.
After clicking the ‘SAP Gateway Client’ button you will be redirected to the Gateway. Here, choose entity set and execute.
Now all processes are done!
You can see all the fields that were added in our view shown in the OData service. To look at the service in the browser click the ‘Response in Browser’ button.
To conclude, by following the steps above you’ve learned how to create and extend a CDS view as well as how to create an OData service using your CDS view with annotation ‘@OData.publish: true’.