Skip to Content
Technical Articles
Author's profile photo Andrea Schlotthauer

New type of CDS entity available: CDS projection views

Since ABAP release 7.76, there’s a new CDS entity available: the CDS projection view. This blog post explains what a CDS projection view is, what it does, and which components it consists of.

 

Update July 2022: Since ABAP release 7.83 | SAP BTP ABAP Environment 2102 | ABAP release 7.56, each CDS projection view must have a provider contract.

Syntax:

define view entity PVName
  provider contract {transactional query | analytical_query | transactional_interface }
  as projection on ProjectedEntity
{ 
...
}

Projection views without provider contract are implicitly set to the provider contract TRANSACTIONAL_QUERY. Therefore, the syntax described in this blog post refers to CDS transactional queries.

ABAP Keyword Documentation about provider contracts: PROVIDER CONTRACT

Blog post about types of CDS projection views: CDS Projection Views in ABAP CDS: What’s Your Flavor | SAP Blogs

 

Background on CDS views

There are currently three types of CDS views available: CDS view entities (DEFINE VIEW ENTITY), CDS DDIC-based views (DEFINE VIEW), and CDS projection views. CDS projection views have been designed similarly to CDS view entities. For example, they don’t have an SQL view attached. For further details on the different types of CDS views, see this blog post post about CDS view entities.

What is a CDS projection view and what does it do?

CDS projection views serve a special purpose within the ABAP RESTful Application Programming Model (RAP). They define interfaces on the basis of existing CDS view models. A projection view is always based on exactly one existing CDS view entity or CDS DDIC-based view and exposes a subset of its elements, which are required for a specific business service. A CDS projection view is a CDS view defined using DEFINE VIEW ENTITY AS PROJECTION ON.

The following image shows the role that CDS projection views play in making a business service. Because CDS projection views are always based on existing CDS view models, they can’t directly access a database table.

The image is an adaptation of the image published in the Developer Guide for the ABAP RESTful Application Programming Model.

Syntax of a CDS projection view

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Exposure of SO Projection View'
define root view entity DEMO_SALES_PV_SO_EXP
  provider contract transactional_query
  as projection on DEMO_SALES_CDS_SO
  {
    key     so_key,
            id,
            lifecycle_status as LifecycleStatus,
            buyer_id         as BuyerId,
            created_by,
            @ObjectModel.virtualElementCalculatedBy:
                        'ABAP:CL_DEMO_SALES_VIRT_ELEM_EXIT'
    virtual expiry_date : abap.dats(8),
            _Buyer : redirected to DEMO_SALES_PV_BUPA_EXP
  }

 

The example shows the syntax of a CDS projection view:

  • Annotations (entity annotations, view annotations, and element annotations) are inherited from the projected entity by default, but they can be overwritten. You can also add new annotations.
  • A projection view is defined using DEFINE [ROOT] VIEW ENTITY AS PROJECTION ON.
  • The data source of a CDS projection view can be either a CDS view entity without parameters, or a CDS DDIC-based view without parameters (this is still supported for downward compatibility).
  • In the element list, fields and associations from the underlying projected entity are exposed.
  • New calculated fields can be added with the keyword VIRTUAL (more details below).
  • All elements can be given a new alias name.
  • Associations from the projected entity can either be exposed directly or redirected to a new target (more details below).

Associations in CDS projection views

When CDS projection views were first published, it was not possible to define new associations. This feature was later introduced with ABAP release 7.80. The newly defined associations in CDS projection view have limitations though: they can be defined and exposed in the element list, but they cannot be used to include fields from the association target in the current projection view. Associations in CDS projection views can be used only to model new relationships that can be interpreted by consumer frameworks, such as SADL.

Virtual elements in CDS projection views

Virtual elements are not part of the projected entity – they are newly added elements that are calculated by frameworks, such as SADL, during runtime. Therefore, virtual elements cannot be read with ABAP SQL, ABAP SQL just returns initial values.

For example, in the ABAP RESTful Application Programming Model, a virtual element can be connected to an ABAP class that implements a special virtual element interface by using the framework-specific annotation ObjectModel.virtualElement.CalculatedBy. In that case the value of the virtual element is calculated during runtime by the SADL framework.

In the syntax above, the class CL_DEMO_SALES_VIRT_ELEM_EXIT is used to calculate the expiry date of a sales order item.

For more information, see Using Virtual Elements in CDS Projection Views.

Redirecting exposed associations in CDS projection views

You can expose a CDS association of the projected entity in the element list of a CDS projection view. If the target of the association does not change in the projection layer, the association can be exposed directly. If the target entity is also projected, the association should be redirected to the target projection view, as shown for the association _Buyer in the example above. There are three different keywords available for redirection, depending on the type of association:

  • REDIRECTED TO: redirects a simple association.
  • REDIRECTED TO COMPOSITION CHILD: redirects a composition. The redirected association must be a CDS composition.
  • REDIRECTED TO PARENT: redirects a to-parent association. The redirected association must be a to-parent association.

The image below shows how several views belonging to a business object are projected and redirected.

Redirected%20compositions%20in%20a%20CDS%20projection%20view

Redirected compositions in a CDS projection view

The image is an adaptation of the image published in the Developer Guide for the ABAP RESTful Application Programming Model.

Outlook

Do CDS projection views remind you of consumption views? That’s right: CDS projection views are the RAP pendant to consumption views, and new scenarios within this framework will be designed using CDS projection views. The existing consumption views will still be available, but fewer new ones will be created.

More development on CDS projection views is being done. Soon there’ll be different types of CDS projection views available: general, transactional, and analytical projection views.

To find out more, see our documentation:

 

 

Assigned Tags

      26 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Samir Dutta
      Samir Dutta

      Excellent feature.

      Thanks Andrea.

       

       

      Regards
      Samir

      Author's profile photo Ali Özgec
      Ali Özgec

      Hi Andrea,

      thank you for the post. Can you please tell me if this feature available in s/4 hana 2020 release?

      thanks in advance.

      Best regards,

      Ali Özgeç

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Ali,

      yes CDS projection views are available on-premise starting with S/4HANA 1909. They were first shipped with ABAP platform cloud 1905.

      Best,

      Andrea

      Author's profile photo Guru Ayarekar
      Guru Ayarekar

      Hi Andrea,

      Thanks for the post.

      How to check( find out )ABAP version associated with the release.

      Thanks in advance.

      Best regards,

      Guru Ayarekar

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Guru Ayarekar,

      here's a mapping of on-premise releases: ABAP Keyword Documentation (sap.com). This is the only external resource available.

      If you're asking about ABAP release 7.76, which is mentioned in this blog, here's a mapping from an internal resource:

      Changes%20in%20Releases%207.7x

      Changes in Releases 7.7x

       

      Hope this helps.

      Best,

      Andrea

      Author's profile photo Minh Tri Nguyen
      Minh Tri Nguyen

      I get a weird error message "You don't have a right to view this website", when I open the Developer Guide for the ABAP RESTful Application Programming Model. Please check and advise

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Sorry, my bad. I've updated the links to the Developer Guide for the ABAP RESTful Application Programming Model and they should work now.

      Thanks for notifying.

      Author's profile photo Sebastien Phan
      Sebastien Phan

      Hi Andrea,

       

      interesting post, I just take a chance here: are the projection views or entity views support propagation of access layer? i.e. if I have a CDS view that uses an underlying CDS with authority check in access layer?

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Sebastien,

      your question is whether access control specified using the annotation AccessControl.authorizationCheck is inherited in a CDS projection view, correct?

      The answer is no, access control is not inherited in CDS projection views. This is described in the ABAP Keyword documentation in the topic about annotations in CDS projection views.

      Quote:

      • "Projection views inherit the field and association annotations of the projected entity. View annotations are not inherited and must be specified anew, if required."

      AccessControl is a view annotation and therefore, it is not inherited and must be specified anew.

      Best,

      Andrea

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      This is an old comment but I'm just throwing some information here because this is the most relevant comment I found in Google search for my predicament.

      As Andrea correctly noted, there is no access propagation in general. However, in projection views, it can be inherited. In the access control, we use the following syntax:

      define role ZC_... {
      grant select on
      ZC_...
      where inheriting conditions from entity ZI_...;
      }

      If we try to define access control for a projection view using the same syntax as we'd use in a regular view (e.g. where... aspect pfcg_auth ...), then we get a mystery error message "Unsupported statement in context of transactional projection view".

      I didn't find any mentions of this error online and had no success using Help link provided by Andrea (it's a very long and convoluted document, I got completely lost in it after about 5 minutes). Had to ask a colleague about this.

      I hope this information helps someone who is searching for the same information. FYI Andrea Schlotthauer

      Author's profile photo Osman Yavuz
      Osman Yavuz

      Hi Jelena,

      Thank you so much for the solution! It worked for me!

      This was the only answer for the error message in entire Google search.

       

      Author's profile photo Andile Khanyile
      Andile Khanyile

      Hi Andrea and colleagues,

      Is it possible to create a projection view for a consumption cds view with input parameters?

      I have created a projection view on standard CDS view 'C_OverdueAcctRbls' in order to then add additional fields as virtual elements.  However when I activate my projection view I get error messages stating that the entity 'C_OverdueAcctRbls' requires input parameter '<p_param1>' ... '<p_param2>'  ...  etc...

      How can I map the input paramers to the standard CDS view in my projection view?

      I hope to hear from you soon.

      Thank you.

       

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Andile Khanyile,

      the syntax for passing input parameters in CDS projection views is as follows:

      ...

      define root view entity ...

      as projection on DEMO_CDS_SCARR_FROM_ABAP_CLASS( <param1> : <value>, <param2> : <value>, ... )

      {

      ...

      }

      Please note the following:

      C_OverdueAcctRbls is an analytical query and it is not recommended to build views on analytical queries. The reason is that analytical queries are not intended to be run on HANA database.

      It would be advisable to build your query on I_OverdueAccountsReceivables directly. However, analytical projection views are currently under construction and your query on I_OverdueAccountsReceivables would need to be a CDS view or CDS view entity.

      Also, please note that C_OverdueAcctRbls and I_OverdueAccountsReceivables are not released under C1-stability contract. That means that the interface might be subject to incompatible changes in the future.

      Best

      Andrea

      Author's profile photo Andile Khanyile
      Andile Khanyile

      Hi Andrea,

      Thanks a lot for the comprehensive feedback.

      I will continue exploring my options in line with your suggestions and take the best approach to meet the business requirements.

      Take care & keep well!

      Author's profile photo divya Bhatnagar
      divya Bhatnagar

      Hi Andrea,

       

      I have similar requirement where I am trying to Create projection view on Custom CDS with Parameter as below -

      define root view entity ZTEST_PROJECTION as projection on ZI_TEST(p_referencedate: '20210101')
      {

      Key Partner

      }

      But getting Below Error in Syntax -

      Unexpected word ( (the keyword { was expected)

      Looks like Parameterized CDS are not allowed in Projection View.

      Could you please share your thoughts on this.

      Take Care and Be safe 🙂

       

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi divya Bhatnagar,

      parameterized CDS work since release 2005CE (NW 7.80).

      If you work on an older release, such as OnPremise NW 7.45, 1909CE, parameter binding  in a CDS projection view is not possible.

      Best

      Andrea

      Author's profile photo Luis Frey
      Luis Frey

      Hi Andrea,
      can you show an example from practice.
      It doesn't make sense like that... Always 100000.. then i can leave parameters right away.

      The parameter must be passed instead of the 100000.

      But if I include the parameter in the projection view, I get this error.

      Thanks for your Help!

      bye,

      Luis

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Luis,

      input parameters are not supported in transactional projection views. That's why you get the error message in the second screenshot.

      You might define a CDS view entity instead of a CDS projection view.

      However, transactional OData Services currently do not support parameters. Even if you define a normal CDS view entity instead of a CDS projection view, it will be ignored in your OData service.

      Best

      Andrea

       

      Author's profile photo Luis Frey
      Luis Frey

      Hi Andrea,

      thanks for that quick answer.

      Hmm not good for my requirement. :-/

      bye Luis

       

      Author's profile photo Jörg Rose
      Jörg Rose

      Is there a way that key users can build analytical projection views instead of regular DDIC based views with an app? The analytical query Fiori app seems to always generate DDIC based queries. And using ADTs in Eclipse is not an option for key users.

      The DDIC based key user queries seem to cause significant activation performance issues in larger deployments.

      Author's profile photo Harish Babu M
      Harish Babu M

      Hi Jorg,

      We are developing a new fiori app where key user can model analytical projection views.

      Regards,
      Harish

      Author's profile photo Jörg Rose
      Jörg Rose

      Hi Harish,

      that sounds awesome. May there also be a way to take analytical queries (at least on projection views) out of the activation dependencies of the underlying composites or have something like a lazy activation for them? Making changes to data models and being agile in reporting may otherwise be quite an issue and a blocker for adoption. BW queries did work that way I guess.

      Thanks and Best

      Jörg

      Author's profile photo Pawan Akella
      Pawan Akella

      Hi Andrea Schlotthauer ,

      Regarding the redirecting the exposed associations,

      the element buyer_id used in the on-condition between projected entity DEMO_SALES_CDS_SO and view entity DEMO_SALES_CDS_BUPA

      since the buyer_id in the projection view is changed using alias BuyerID we are using redirected to the projection view of DEMO_SALES_CDS_BUPA

      Is this correct ?

       

       

       

      Thanks

      Akella

      Author's profile photo Fabian Fellhauer
      Fabian Fellhauer

      Hi Akella,

      by using the REDIRECT feature, references in the on-condition to aliased elements are automatically adapted. The REIDRECT feature is intended to re-use existing associations defined in the base entity, instead of defining the association again in the projection layer.

      Hope this helps!

      Thank you and best regards

      Fabian

      Author's profile photo Michael Smith
      Michael Smith

      Hi Andrea Schlotthauer ,

      Great blog!  Thanks for sharing!  One question: is it possible to develop a projection view on top of a simple CDS view - with no root or parent associations?  I tried to create a projection view on interface view I_MEMORECORDBASE (which is just a simple select on the FQM_FLOW table), but I get the following error: "Basis entity must have a ROOT property or a TO PARENT association".

      Thanks,

      Michael

      Author's profile photo Andrea Schlotthauer
      Andrea Schlotthauer
      Blog Post Author

      Hi Michael,

      as of 2102, a projection view needs a provider contract. The provider contract can be:

      • TRANSACTIOAL_QUERY
      • TRANSACTIONAL_INTERFACE
      • ANALYTICAL_QUERY

      If you don't specify any provider contract, the projection view is interpreted as transactional_query by default. A transactional query must be part of a RAP BO and therefore, you need either the keyword ROOT or a to-parent association.

      A projection view can be either part of a RAP BO or otherwise, part of an analytical query (ANALYTICAL_QUERY). In this case, you have other requirements.

      Described here: CDS Projection Views in ABAP CDS: What’s Your Flavor | SAP Blogs

      Hope this helps.