Friday, 19 November 2021

CDS View Extension and OData Exposing

CDS(Core Data Services) is one of the new technologies SAP provides us. Similar to AMDP, it aims to increase performance by enabling data models to use and define in the database level instead of the application level. Although it has a wide range of uses, I will explain to extend a basic CDS view and expose it as an OData service.

CDS Views are virtual data models and they can be used to access Hana tables. You can use standard CDS views or create a custom one. However, in case of need, you may want to extend an already existing view to add new fields instead of creating a new one. Now, in this blog, I will create a basic CDS view, show how to extend it, and use as an OData service.

◉ CDS View Extension

Firstly, we need to launch Eclipse ABAP Development Tools and search for ‘Data Definiton’ and click next.  After that, we create our first view, since it will be a basic view we need to choose Define View template and click finish.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Now our first view is generated. In the next step, we will add the DB table and its fields to get the data, and run the program to see results.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

As clearly seen, our view contains only the fields we added and data we choose with where condition.

In the third step, we will create another view to extend our basic view. With following the same way we will create a new data definition but differently, the template should be chosen as Extend View this time.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Our second view is also generated and there is only one step left, instead of ‘view_name‘ we define the name of the view we want to extend and the fields to be used in the extension.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Now, this part is also done, after activating this program we can turn back to our basic view and run to see the difference.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Before the extension, our custom view had 5 fields and now it has 3 additional fields.

◉ Expose CDS Views as OData Services.

 
In the first part of this blog, I explained CDS view extension and now I’ll show how we can create an OData service using our view. There are actually 3 ways to create an OData service with CDS views but I will prefer to use @Odata.publish annotation since it does not require SEGW transaction as the traditional way.

We firstly need to add ‘@Odata.publish: true’ annotation to our basic view.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Then launch SAP Gui, go to /n/IWFND/MAINT_SERVICE transaction and click ‘Add Service’.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

We will then be redirected to a new page, where we want to find our backend service, and to do that, we need to write ‘local’ to System Alias and the name of our CDS view to Technical Service Name.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Once we found our backend service, we click on it and make the package assignment, and click continue.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Now our service is also ready, we can turn back to /n/IWFND/MAINT_SERVICE transaction and find our service using ‘Filter’.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

After clicking the ‘SAP Gateway Client’ button we will be redirected to Gateway. We just need to choose the entity set and execute.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

Now all processes are done and we can see all fields added in our view are shown in OData service. To look the service in browser we can click ‘Response in Browser’ button.

OData, ABAP Development, SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Exam, SAP ABAP Preparation, SAP ABAP Skills, SAP ABAP Jobs

To conclude, we learned how to create and extend a CDS view. We also learned how to create an OData service using our CDS view with ‘@OData.publish: true’ annotation.

Source: sap.com

No comments:

Post a Comment