Skip to Content
Author's profile photo Pavankumar Reddy

Expose CDS Views as OData Service

In continuation to previous blog Core Data Services in ABAP in this blog I will show how to create CDS Views and step by step procedure to generate OData service using CDS Views.

 

There are 3 different ways you can expose CDS views as OData service:

  1.   Import DDIC structure using SEGW Netweaver Gateway service builder transaction.
  2.   Reference Data Source using SEGW Netweaver Gateway service builder transaction.
  3.   Using Generic Annotations. (@Odata.publish:true)

 

First way is supported from SAP ABAP NW 7.40 SP5, Second and third ways are supported from SAP ABAP NW 7.50 and above. For first two ways SAP NW Gateway is used and for third type without use of SAP NW Gateway odata services can be created using annotations. Gateway is used only to add the service created using annotations.

 

In this blog I am going to show type one i.e how to generate OData service using import DDIC structure using SEGW service builder transaction.

 

How to Create CDS View:

 

Firstly as a prerequisite ABAP Development tools(ADT) needs to be installed in Eclipse to create CDS Views.

 

On installation of ADT in eclipse open ABAP perspective as shown below:

/wp-content/uploads/2016/04/o1_911924.png

 

 

After opening ABAP perspective, In project explorer right click in the context menu select New->ABAP Project. A popup appears asks for ABAP system details which you need to add to create CDS views. Details like System-ID, Username, Password has to be given and click on OK to add the system to eclipse. Note that ABAP system which you add should support SAP ABAP NW 7.40 package. find the attachment for how to add ABAP system to eclipse.

 

Once after adding the ABAP system project explorer looks like below:

 

/wp-content/uploads/2016/04/o2_911925.png

 

Expand $TMP package, Select and right click on Dictionary folder in the context menu to create DDL source ABAP Repository Object

/wp-content/uploads/2016/04/03_911936.png

Select DDL source, click on next and give below details to create sample CDS view:

/wp-content/uploads/2016/04/o4_911937.png

 

Without clicking on finish, on click on next CDS view ask for transport:

 

/wp-content/uploads/2016/04/05_911938.png

 

since CDS view is create in local object no transport is required, click on next then sample templates are populated to select:

/wp-content/uploads/2016/04/1_911945.png

 

You can use any of the template as shown above to create CDS view. Template helps you to create different types of CDS views with syntax auto populated on selecting the type of view. Here I am creating simple CDS view so I select define view template and click on finish.

 

/wp-content/uploads/2016/04/1_911945.png

In the define view template core annotations are populated with basic syntax to create view, define the view as below:

 

/wp-content/uploads/2016/04/1_911945.png

 

 

Here I defined sql view name as ‘CDS_DDL_SAMPLE1’ and selected data from “snwd_so” sales order data source with some columns. After defining the view, Activate the view as shown above. On Activation two objects are created:

     1. CDS Database View (CDS_DDL_SAMPLE1)

     2. CDS entity. (ZCDS_SAMPLE1)

 

CDS Database view can be accessed using SE11 ABAP Dictionary as shown below:

 

/wp-content/uploads/2016/04/1_911945.png

 

Give CDS Database View name as shown above and click on display.

 

/wp-content/uploads/2016/04/1_911945.png

DDL SQL view is created in ABAP Dictionary. Here you can identify MANDT field in the view definition though we didnt mention it explicitly while defining the view. CDS views are default client dependent no need to mention client field while defining the view.

 

Right click on CDS View created to preview the data as shown below:

/wp-content/uploads/2016/04/2_923187.png

 

Generate OData service using CDS View:

 

Go to SEGW transaction. Create project as shown below:

/wp-content/uploads/2016/04/1_911945.png

Give project details as below:

/wp-content/uploads/2016/04/1_911945.png

Save project in local object. Expand project node, right click on data model->import->DDIC structure:

/wp-content/uploads/2016/04/1_911945.png

Create entity type by importing ABAP structure. Here give  CDS Dabase view ‘CDS_DDL_SAMPLE1’, select create default entity set flag and click on next:

/wp-content/uploads/2016/04/1_911945.png

 

follow the wizard where you select parameters and key fields respectively:

/wp-content/uploads/2016/04/1_911945.png

 

/wp-content/uploads/2016/04/1_911945.png

click on finish, then entity, entity set and corresponding properties are created. Expand node service implementation right click on SalesOrderSet->Map to Data Source

/wp-content/uploads/2016/04/1_911945.png

Here you map your CDS Database view(CDS_DDL_SAMPLE1) and CDS enity(ZCDS_SAMPLE1). on click on map to data source popup populates to select CDS business entity:

/wp-content/uploads/2016/04/1_911945.png

 

select type as business entity and give CDS entity ZCDS_SAMPLE1 as Name:

 

/wp-content/uploads/2016/04/1_911945.png

 

click on OK.

 

/wp-content/uploads/2016/04/1_911945.png

click on OK. Drag and drop properties to map fields or click on generate mapping to automatically map the fields.

/wp-content/uploads/2016/04/1_911945.png

After mapping done save the changes and click on generate icon as shown below to generate run time artifacts./wp-content/uploads/2016/04/1_911945.png

 

In model and service definition you can find DPC, MPC classes and technical service name of odata service:

/wp-content/uploads/2016/04/1_911945.png

click on ok and save the objects in local object;

/wp-content/uploads/2016/04/1_911945.png

after saving run time artifacts are generated. Expand node Service maintenance select gateway, click on register button to register the service.

/wp-content/uploads/2016/04/1_911945.png

add the service to local object:

/wp-content/uploads/2016/04/1_911945.png

click on OK. Now service is registered you can find status as green as shown below and click on Gateway client button to access the service:

/wp-content/uploads/2016/04/1_911945.png

Odata service in gateway client:

/wp-content/uploads/2016/04/1_911945.png

Use OData parameter $metadata to view the metadata of service:

/wp-content/uploads/2016/04/1_911945.png

 

on using odata parameter $metadata you can find the properties and entities of CDS view exposed as odata service.

 

This is how you create CDS view and expose the same as odata service using import DDIC structure by mapping CDS Database View and CDS entity using SEGW service builder transaction.

 

Hope you find this blog helpful.

 

In my next blogs I will try to touch other ways to expose cds view as odata service.

 

Your suggestions, feedback, comments on this blog are most welcome.

Few Useful links:

Step by Step Hierarchies in S/4 HANA Analytics

Define the Analytical Query CDS View

http://scn.sap.com/community/gateway/blog/2016/06/01/odata-service-development-with-sap-gateway-using-cds-via-referenced-data-sources

http://scn.sap.com/community/abap/blog/2016/03/10/my-cds-view-self-study-tutorial–part-1-how-to-test-odata-service-generated-by-cds-view

Note: This is my previous SCN for the new and latest blogs please follow my new SCN blog https://people.sap.com/reddypavankumar

Assigned Tags

      38 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kiran Padarthi
      Kiran Padarthi

      Good one 🙂

      Author's profile photo Former Member
      Former Member

      Good Info ...

      Author's profile photo Former Member
      Former Member

      Previously i was saw some partial information , here i got what i am expected.

      Author's profile photo Former Member
      Former Member

      Ohh really great forum Pavan. Great to know CDS view and to learn it easily . Thanks for sharing precious knowledge. 🙂

      Author's profile photo Former Member
      Former Member

      Good one.. helpful 🙂

      Author's profile photo Former Member
      Former Member

      Nice blog... very informative

      Author's profile photo Ravindra kalyan Vallabhuni
      Ravindra kalyan Vallabhuni

      Good one. Keep Posting.

      Author's profile photo Ramesh Kothapally
      Ramesh Kothapally

      Nice Blog ... 🙂

      Author's profile photo Thomas Fiedler
      Thomas Fiedler

      Hi,

      in NW 7.50 you can remove 80% of your blog 🙂

      You just use the CDS annotation: @OData.publish: true and the oData Service will be generated automatically by the ABAP infrastructure. There is no need to use SEGW to expose CDS views as services anymore.

      Regards,

      Thomas.

      Author's profile photo Pavankumar Reddy
      Pavankumar Reddy
      Blog Post Author

      Hi Thomas,

      Yes your true. This blog will help to one's who are still using NW 7.40 🙂

      Regards,

      Pavan

      Author's profile photo Former Member
      Former Member

      hi Pavankumar

      My situation is Front end server NW750,back -end server NW740.

      How should i do?

      thanks.

      York Liu

      Author's profile photo Pavlo Astashonok
      Pavlo Astashonok

      My situation is Front end server NW750

      What is "your frontend server"? SAP has no separate server for frontend

      Author's profile photo Reji Konnasseri
      Reji Konnasseri

      Hey Pavan,

       

      Understand this is an age-old blog, but obviously, we still have some clients on 7.4. I had to do something similar, but my CDS view have associations. When I import using that dictionary structure, it only picks up the fields from the first table and ignores all the associations in the CDS view. Wonder if this is how it will work or is there a method to extract the association from the CDS view?

      Please advise.

      Thanks,

      Reji

      Author's profile photo Former Member
      Former Member

      Hi Thomas,

      I am using NW 7.50 and i created the OData Service by using the CDS annotation: @@OData.publish: true. The service was successfully created and after that i activated the OData Service in SAP GATEWAY HUB using Transaction- /IWFND/MAINT_SERVICE.

      When i test the service its working fine. But when i tried to check service implementation of the EntitySet i mapped to by going to SEGW Tcode i dont find my service there.??

      Could you please help me on this?

      Thanks,

      Rahul.

      Author's profile photo Rahul Datta
      Rahul Datta

      Jus to add, you still need SEGW in NW7.5 if you want to create one OData service for multiple CDS's.

      Author's profile photo Zeshan Shafqat
      Zeshan Shafqat

      i think in NW we can create directly

      Author's profile photo Former Member
      Former Member

      "after saving run time artifacts are generated. Expand node Service maintenance select gateway"

      but,there is no gateway in node Service maintenance.

      why?

      thanks.

      York Liu

      Author's profile photo Pavankumar Reddy
      Pavankumar Reddy
      Blog Post Author

      You have to configure gateway client as prerequisite

      http://scn.sap.com/docs/DOC-41416

      Refer above link It would solve your problem.

      Regards,

      Pavan

      Author's profile photo Former Member
      Former Member

      hi Pavankumar

      This issue is solved.thank you very much.

      and then How to Expose CDS Views as OData Service in Front-end server is NW750,back-end server is NW740.

      York Liu

      Author's profile photo Andre Fischer
      Andre Fischer

      Hi York,

      since service development takes place in the backend you have to use the approach

      Pavankumar has described.

      Regards,

      Andre

      Author's profile photo Andre Fischer
      Andre Fischer

      you don't have to work in SAPGUI in a separate windows but you can also use SEGW from within ABAP in Eclipse.

      Using SEGW and ABAP in Eclipse

      Author's profile photo Former Member
      Former Member

      Hi,

      I created OData service by mapping to a CDS view as data source, my question is if I make any changes in the CDS view, like if add or remove any field, would the changes be reflected in the data model as well??

      Author's profile photo Pavankumar Reddy
      Pavankumar Reddy
      Blog Post Author

      Yes changes will be reflected. To reflect changes you need to activate CDS entity, Generate run time artifacts again.

      Author's profile photo Andre Fischer
      Andre Fischer

      Hi Pavankumar,

      sorry, but this is not correct.

      If a service has been built using the mapped data source approach, you have started with importing a DDIC structure.

      If additinal fields are added this is NOT reflected in the service strucuture and implementation.

      If you want to have this behaviour you have to use the referecned data source approach intaed.

      Best Regards,

      Andre

      Author's profile photo Michal Szczerbowski
      Michal Szczerbowski

      Ok, but as mentioned above 'reference' approach is available only in 7.50.

      So in 7.40 you need to expose the CDS to a new odata?

      Author's profile photo Andre Fischer
      Andre Fischer

      not a "new" odata. But you have to use the option 1 as described above and map the CDS View to the entity set created via DDIC import

      "Import DDIC structure using SEGW Netweaver Gateway service builder transaction"

      Best Regards,
      Andre

      Author's profile photo Former Member
      Former Member

      Hi Pavan ,

      we are running our system on NetWeaver 7.4 with S4 hana version 1503 . our requirement to show the standard reports from fiori with backend as s4 hana system . so I collected the list of the standard app developed for the 1503 version from fiori library and I could find the names of the Odataservices also .
      when I am trying to find the source of these Odataservices I couldnt trace with SEGW , I also try to search with eclipse which didnt help me more .
      Can you help me in providing some clue to more forward...?
      Do I need to to install any package to see the list of the CDS views in the ABAP repository using eclipse..?

      Regards,
      Madhu

      Author's profile photo Former Member
      Former Member

      Very useful tutorial, was reading and following another tutorial and wondered why ABAP Developer Tools in Eclipse didn't create oData service in SAP... Was working on NW7.40 SP10 😉 Great help, thanks!

      Author's profile photo Former Member
      Former Member

      Really helpful. Especially when Annotations '@Odata.publish:true' doesn't work... for NW7.40.

      Thank you so much!

      Author's profile photo Pavlo Astashonok
      Pavlo Astashonok

      ha-ha)))

      Author's profile photo Vijay Chintarlapalli
      Vijay Chintarlapalli

      Really Very useful one I have been searching and trying to do this for the work but was stuck in how to call the CDS view but this tutorial clearly focused on the required part for the SAP HANA and UI5  developer.

       

      Author's profile photo Ashish Trivedi
      Ashish Trivedi

      Very nice info. 🙂

      Author's profile photo Thirumoorthy Rajaram
      Thirumoorthy Rajaram

       

      Hi Pavan,

      Very nice blog and thanks for sharing the knowledge. Could you please brief on this point as well - Reference Data Source using SEGW Netweaver Gateway service builder transaction.

      Best Regards,

      Thiru

      Author's profile photo Andreas Michaelbach
      Andreas Michaelbach

      Thank you very much. I hope 7.50 will be  available on Solution Manager 7.2 soon...

       

      Author's profile photo Marcelo Rodrigues
      Marcelo Rodrigues

      Hello, the service maintenance option is not available for registration according to figure 26.

      Author's profile photo Marcelo Rodrigues
      Marcelo Rodrigues

      I found the solution: https://blogs.sap.com/2013/05/30/system-alias-and-routing-of-request-in-sap-netweaver-gateway/

      Author's profile photo Zeshan Shafqat
      Zeshan Shafqat

      Nice work Brother

      Author's profile photo Sivapuram Phani Kumar
      Sivapuram Phani Kumar

      Hello Pavankumar Reddy - The blog is detailing each step needed to create oData Service using CDS views. I am able to create the Service and successfully check the metadata and all. However, I am facing challenges in seeing the actual data. May be the question is not relevant or linked with CDS view but related to oData Services. Can you help me understand how I can view the data? How I can generate a feed URL that I can use for Power BI reporting?

      Appreciate your feedback.

       

      Thanks,

      PS