cancel
Showing results for 
Search instead for 
Did you mean: 

SAP Application Programming Model - How to reuse / mixin Fiori annotations?

former_member182048
Active Contributor

I have a Fiori service which has lots of views which share many of the same properties

Currently i am repeating the same boiler plate annotations over and over again

In my Fiori Annotations .cds file i would like to be able to create my own type and then apply it where ever

from this

annotate cats.StocksView1 with @( // header-level annotations	
..
}){ 
 // element-level annotations	
 ID @UI.Hidden;	 
 CreatedAt @UI.Hidden;
 Modified @UI.Hidden;
}
...
annotate cats.StocksViewN with @( // header-level annotations	
..
}){ 
 // element-level annotations	
 ID @UI.Hidden;	 
 CreatedAt @UI.Hidden;
 Modified @UI.Hidden;
}

to

"@myAnno": { 
 ID @UI.Hidden;	 
 CreatedAt @UI.Hidden;
 Modified @UI.Hidden;
}
annotate cats.StocksView1 with @(myAnno)
..
annotate cats.StocksViewN with @(myAnno)

I am guessing the answer is probably in the help somewhere, but anyone know off the top off their heads if it could work like that?

faresletaief
Explorer
0 Kudos

Hello Jhon,

I'am facing the same requirement to reuse fiori annotation did you find way to acheive this ?
thank you

Accepted Solutions (1)

Accepted Solutions (1)

chgeo
Advisor
Advisor

Hi John 🙂

just add the common annotations to an own entity or type from which others can inherit:

type managed {
  createdAt  : DateTime @UI.Hidden;
}
entity Books : managed {}

See the docs.

Seems the predefined CDS types already cover what you want to achieve. Take a look at the 'managed' and the 'cuid' types in 'node_modules/@sap/cds/common.cds'. Refer to them with

using { managed, cui } from @sap/cds/common;

Available with @sap/cds >=3, documentation soon to come.

Answers (2)

Answers (2)

former_member182048
Active Contributor
0 Kudos

thinking about it I think i should have started with an Abstract Entity, then used that as a base type for all the child entities

abstract entity MyTable @(
  UI: {
   Identification: [{ $Type: 'UI.DataField', Value: name }],
   PresentationVariant:
    { SortOrder: [ {$Type: 'Common.SortOrderType', Property: Field1, Descending: false}], 
      RequestAtLeast: [Field1,Field2] 
    },
  LineItem: [ 
    {$Type: 'UI.DataField', Value: Field3, "@UI.Importance":#High},
    {$Type: 'UI.DataField', Value: Field4, "@UI.Importance": #High},
    {$Type: 'UI.DataField', Value: Field5, "@UI.Importance": #High},
 ) {
    key Field1 : String(3);   
        Field2: localized String;
        Field3: localized String;
  ...

  }

then I can use

entity Child1 : MyTable {
	FieldN: String(40) @title: '{i18n>FieldN}';
}
entity Child2 : MyTable 

just saw it used here via foundations reuse example

https://github.com/SAP/cloud-samples-catalog/blob/master/db/model.cds

former_member182048
Active Contributor
0 Kudos

Cheers Christian

You answered my question correctly, and you definitely pointed me in the right direction, I think however i may have asked it wrongly, I'll try again

In my scenario i have 10+ views, the views share a lot of the same characteristics, not all and the data is different,

All the views need the same UI Annotations, example below being a @UI.PresentationVariarnt and LineItem

UI: {
 PresentationVariant:
  { SortOrder: [ {$Type: 'Common.SortOrderType', Property: Field1, Descending: false}], 
    RequestAtLeast: [Field1,Field2] 
},
  LineItem: [ 
    {$Type: 'UI.DataField', Value: Field3, "@UI.Importance":#High},
    {$Type: 'UI.DataField', Value: Field4, "@UI.Importance": #High},
    {$Type: 'UI.DataField', Value: Field5, "@UI.Importance": #High},

..
   ]
 }

rather than repeating that code many times like below, or doing f

annotate Child11 with @(
UI: {
 PresentationVariant:
  { SortOrder: [ {$Type: 'Common.SortOrderType', Property: Field1, Descending: false}], 
    RequestAtLeast: [Field1,Field2] 
}
...
}
..
annotate Child1N with @(
UI: {
 PresentationVariant:
  { SortOrder: [ {$Type: 'Common.SortOrderType', Property: Field1, Descending: false}], 
    RequestAtLeast: [Field1,Field2] 
}
...
}

is there a way to do this without repetition?