Read Simple field metadata from View Configuration
Recently, I came across a requirement where I had to read some field metadata from view configuration (say for instance Field Name & Field Label)
Since I couldn’t find a ready-to-use “SAP-Standard” way to do this, I had to tweak some of the view configuration related methods to achieve this. I would like to share the same via this post.
1. Create a simple transformation for reading the fields from a view configuration. Let me call this as zz_tr_field_list.Code snippet shown below.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sapxsl="http://www.sap.com/sapxsl"
xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<asx:abap><asx:values><xsl:element name="FIELDS">
<!-- get mandatory fields from form configuration -->
<xsl:for-each select="//gridcell/*[@id]">
<xsl:element name="FIELDS">
<xsl:value-of select="@id"/>
</xsl:element>
</xsl:for-each>
</xsl:element></asx:values></asx:abap>
</xsl:template>
</xsl:transform>
2. Using the configuration manager and configuration descriptor classes, call the above transformation to get the fields. I am pasting here below a small sub-routine (which we will call in a later step).
FORM read_fields_from_config USING
iv_target_view_name TYPE string
iv_role_key TYPE bsp_dlc_role_key
iv_object_type TYPE bsp_dlc_object_type
iv_sub_object_type TYPE bsp_dlc_object_sub_type
iv_component_usage TYPE bsp_dlc_component_usage
CHANGING lt_ums_field TYPE STANDARD TABLE.
DATA : lt_current_view_fields TYPE bsp_dlct_field,
lt_table_view_fields TYPE bsp_dlc_column_descr_tab,
ls_table_view_fields LIKE LINE OF lt_table_view_fields.
TRY.
* Get configuration manager for business role
CREATE OBJECT lr_configuration_manager
TYPE
cl_bsp_dlc_config_manager2
EXPORTING
iv_role_key = iv_role_key.
* ... takes role configuration key and ui object type into account
lr_result = lr_configuration_manager->get_configuration( iv_viewname = iv_target_view_name
iv_view_comp_id = 'C16_W61_V62' ) ##no_text.
IF iv_component_usage IS NOT INITIAL.
CALL METHOD lr_result->set_component_usage
EXPORTING
iv_component_usage = iv_component_usage.
ENDIF .
lr_configuration_descriptor2 ?= lr_result.
lr_configuration_descriptor2->set_object_type( iv_object_type ).
lr_configuration_descriptor2->set_object_sub_type( iv_sub_object_type ).
CALL METHOD lr_result->get_config_data
RECEIVING
rv_result = lv_configuration_xml
EXCEPTIONS
foreign_lock = 1
config_not_found = 2
OTHERS = 3.
IF sy-subrc <> 0.
RETURN.
ENDIF.
* ... Perform XML transformation
* -> Result is list of all fields/headings
TRY.
CALL TRANSFORMATION zz_tr_field_list
SOURCE XML lv_configuration_xml
RESULT fields = lt_result.
CATCH cx_root.
RETURN.
ENDTRY.
3. The above call transformation will work very fine for an Overview Page. So what if we have a table view? Here’s the code that follows.
IF lt_result IS INITIAL. "Possibly Table View...
lt_table_view_fields = cl_bsp_dlc_table_utility=>get_table_from_config_xml( lv_configuration_xml ).
ENDIF.
4. Now we are almost done. We still need to get the Field Labels. Let us use the following code to achieve this.
* Get names of configured search criteria
lr_view_descriptor = lr_result->get_property_descriptor( ).
IF lt_result IS NOT INITIAL.
LOOP AT lt_result INTO lv_result.
CHECK lv_result CP '//*'. "could be a guid of heading
ls_field-field_name = lv_result. "example for field name //HEADER/STRUCT.FIRSTNAME
APPEND ls_field TO lt_current_view_fields.
ENDLOOP.
ENDIF.
IF lt_table_view_fields IS NOT INITIAL.
LOOP AT lt_table_view_fields INTO ls_table_view_fields WHERE name NE 'THTMLB_OCA'. "#EC CI_STDSEQ
ls_field-field_name = ls_table_view_fields-title.
APPEND ls_field TO lt_current_view_fields.
ENDLOOP.
ENDIF.
* Determine labels
lr_view_descriptor->get_field_labels( CHANGING ct_fields = lt_current_view_fields ).
5. Let us see how to use the above snippet in a report program.
Eg: We would like to get the field metadata for Account Details View. We set the required parameters for the above sub-routine and then call it as seen below.
lv_target_view_name ='BP_HEAD/AccountDetails'.
lv_role_key = <<Give Role Config Key here>>.
lv_object_type = 'BP_ACCOUNT'.
lv_sub_object_type = 'INDIVIDUAL'.
lv_component_usage = 'BP_HEAD'.
PERFORM read_fields_from_config USING lv_target_view_name
lv_role_key
lv_object_type
lv_sub_object_type
lv_component_usage
CHANGING lt_ums_field.
Hope this was a useful read..!!
Hi Nisha,
Nice one. Keep posting....
Thanks,
Faisal