Create and Consume a custom CDS view in SAP S/4 HANA Cloud through API
You need to develop a custom Core Data Services (CDS) view in S/4 HANA cloud. Data from this view should be accessible from outside S/4 HANA cloud through a whitelisted API.
This scenario can be useful in cases when it is required to access data outside S/4 HANA cloud and there is no whitelisted API (Application Programming Interface) to access data directly from the cloud system. However, if there are standard CDS views available that hold the required data, a custom CDS view can be built on top of these standard views to fetch only the required data and then this view can be exposed to the outside world through an API. This data can then be used as required by the external system, for reporting or analytical purposes as an example, or for further transactions/analysis as need be. The scenario is even more useful when the standard CDS view are not accessible to external world through an API URL.
1. Build the Custom CDS view
On the S/4 HANA cloud home page, Search for the “Custom CDS View” App as shown:
Click on the App displayed
The system will display the list of Standard CDS views available. Click on the “Create” button to create a new view.
Provide a name and label for this new CDS view. Also ensure to check the “External API” checkbox. This will enable the view to be exposed to external applications for consumption.
Now we need to specify a data source for our newly created view. In the lower section of the screen, you can see that there are Primary and Associated data sources for the view. Click on “Add” and first choose the “Add Primary data source” option to add a new data source to the view.
In this example, we will retrieve the different statuses of the items of an outbound delivery. Therefore, our primary data source will be the “Outbound Delivery” view (named as I_OutboundDelivery).
In the search box, search for the outbound delivery view as shown.
The system shows up the two standard views that exist in the system. Click on the “I_OutboundDelivery” view to select it and then click on the “Add” button to add this view as a primary data source.
The I_OutboundDelivery view is the header level view for outbound delivery. The item-wise status is stored in the “I_OutboundDeliveryItem” view. After adding the primary data source, add this view as the associated data source to the custom view.
In much the same way as the primary data source was added, add the I_OutboundDeliveryItem view as the associated data source.
Once the two data sources are added, we need to define association properties. Click on the “Edit” association properties” button to do so.
On the resulting screen, we need to provide the fields that join the two data sources. In our example, the two data sources are linked by the “OutboundDelivery” field. Choose these accordingly as per the attached screenshot below. Click “OK” once done.
Click on “Save Draft” to save your view. The system might present a dialog confirming addition of an access protected data source. Click “Yes” to continue.
Now that the data sources are added, we need to select the fields that we want to see as part of our view. Click on the “Field selection” tab.
Select the relevant fields from the two data sources (primary and associated). These fields will form the data of the new custom view. In this example, we will choose the Outbound delivery number and document type from the primary data source and the outbound delivery item number along with the Product and the different statuses of the item from the associated data source.
Provide a “Label” for the selected fields, this will appear as the label for the fields of the custom view.
Click on the “Save Draft” button to save your view.
Now that the view is created, click on the Publish button to publish your view. You can also Click on the “Preview” button to view the contents of the view.
Once Published, we need to make some settings to ensure our view is accessible from outside. For this, we will need to have a URL (which will act as an API) to access the view. The following steps need to be performed to do so.
2 2. Create Communication Scenario
On the S/4 HANA cloud home screen, search for the app called “Custom Communication scenarios”.
Click on the app and subsequently click on the “New” button to create a new custom communication scenario.
Provide a name and description for the new custom communication scenario and click on “New”.
In the “Inbound services” tab, click on the “+” icon
Now search for the CDS view created earlier, select it and click on “OK”
This attaches our custom CDS view to the communication scenario. Click on “Publish” to publish this communication scenario.
3. Create communication System
Navigate back to the home screen and search for the “Communication Systems” app. Click on the app to open it.
Click on “New” to create a new communication system.
Provide a System ID and system name and click on “Create”.
Provide a host name. Since we are going to use an inbound service only, this is only hypothetical and it could therefore be any name.
Under “User for inbound communication”, click on the “+” icon to add a new user.
You can use an existing user name or create a new user for this purpose. Choose “User name and password” as the authentication method.
Provide a user name, description. Click on the “Propose password” button to generate a password. It will be displayed on the screen, so it is advisable to store it somewhere separately. Click on “Create” to create the user.
Click on “OK” to assign this user to the communication system.
Save the communication system.
4. Create communication Arrangement
Navigate back to the home screen and search for the “Communication Arrangements” app. Click on the app to open it.
Click on “New” to create a new communication arrangement.
On the subsequent dialog screen, you need to provide the communication scenario and a name for the communication arrangement. For communication scenario, provide the name of the scenario created above (alternatively you can search for it as well).
The arrangement name is taken to be the same as the scenario name and can be edited. In this case we just let it use the default. Click on Create to proceed.
Provide the name of the communication system created in the previous step.
The Service URL gives the URL which can be used to access the CDS view externally. Take note of this URL and store it somewhere in a notepad. Save the Communication arrangement.
5. Test the CDS view
Use the URL and open it in a browser (preferably Chrome). Provide the user name and password provided while creating the communication system.
Now, copy the name of the collection and suffix it to the URL that was opened above.
This will show the data of the view in the browser (not shown here due to confidentiality reasons).
This URL can now be used to be called from, for example, a UI5 application and the data can be accessed by such an application.
I will cover the creation of a simple application to consume this view in a subsequent blog. This blog therefore is just meant to demonstrate how CDS views can be accessed externally, and using a similar approach, more business requirements can hopefully be met.
Great Blog Priyank , very useful
great piece of information on how to access data outside S4HANA cloud.
Great info Priyank, thanks!
nice simply explained article well done
very helpful, thanks.
Thank you for this extensive piece Priyank. Very helpfull!
Informative Blog. Thanks
Good and very detailed.
Very good information Priyank Kumar Jain , i will definitely use this one on the many cases where i have customers and partners asking for more info. Thanks 🙂
Very Useful! Thanks for sharing!
Great blog, Priyank Kumar Jain. Thank you!
Very helpful. Thank you for sharing.
Great blog. When creating a new CDS View, I can see how you can select existing CDS Views as your primary and additional data sources. Do these existing CDS Views cover all the base Tables? If not, can we create new CDS Views to expose a DB Table that is not covered by the existing CDS Views?
Relevant question. Is that allowed to create a custom CDS view accessing directly HANA DB tables?