How to Retrieve Attachment with GW ?
The OData Channel provides a generic solution for exposing binary data that is stored in an SAP Business Suite backend system and should be accessible using a media link entry.
Here the definition of the DPC method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM in order to retrieve the media.
Also, you will find some useful sample of GET_STREAM method implementation.
Table of Contents
Retrieve Attachment with GW : Signature of GET_STREAM
- IV_ENTITY_NAME Importing STRING
- IV_ENTITY_SET_NAME Importing STRING
- IV_SOURCE_NAME Importing STRING
- IT_KEY_TAB Importing /IWBEP/T_MGW_NAME_VALUE_PAIR
- IT_NAVIGATION_PATH Importing /IWBEP/T_MGW_NAVIGATION_PATH
- IO_TECH_REQUEST_CONTEXT Importing /IWBEP/IF_MGW_REQ_ENTITY
- ER_STREAM Exporting DATAES_RESPONSE_CONTEXT Exporting /IWBEP/IF_MGW_APPL_SRV_RUNTIME=>TY_S_MGW_RESPONSE_ENTITY_CNTXT
The last parameter ER_STREAM with contain the Attachment.
/IWBEP/ Prerequis for GET_STREAM
In order to use an entity as Stream, you should make some change in the metadata definition of the service in order to set the Entity Type to Stream.
Go to your Service’s MPC_EXT class and redefine your DEFINE method.
You have to set The property for the EntityType to ‘ContentType’.
Use then lo_property->SET_AS_CONTENT_TYPE.
Here a sample code of how to set the entity EmployeePhoto to Stream in order to send Employee’s photo through SAP GW oData Service: (source)
super->DEFINE( ). DATA: lo_entity type REF TO /IWBEP/IF_MGW_ODATA_ENTITY_TYP, lo_property type REF TO /IWBEP/IF_MGW_ODATA_PROPERTY. lo_entity = model->GET_ENTITY_TYPE( IV_ENTITY_NAME = 'EmployeePhoto' ). IF lo_entity is BOUND. lo_property = lo_entity->GET_PROPERTY( IV_PROPERTY_NAME = 'ContentType' ). lo_property->SET_AS_CONTENT_TYPE( ). ENDIF.
Most common Method GET_STREAM Sample Implementations
Here a sample implementation of the the method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM to Retrieve Attachment (file or picture) using SAP GW oData Service.
Retrieve Attachment (Employee’s Photo) using SAP GW
Maybe the most famous scenario for GET_STREAM method is to retrieve attachment (Photo) of SAP Employee.
Here the implementation of GET_STREAM for EmployeePhoto :Â (source)
DATA: ls_stream TYPE ty_s_media_resource ,lo_api TYPE REF TO if_mr_api ,lv_entity_name TYPE /IWBEP/MGW_TECH_NAME ,er_entity type REF TO DATA ,lr_entity TYPE REF TO DATA ,employeephotoset_get_entity TYPE ZCL_ZMEDIALNK_SB_MPC=>TS_EMPLOYEEPHOTO ,ls_content type table of TBL1024 initial size 0 ,ls_xstring type xstring ,lt_messg type table of BAPIRET2 initial SIZE 0 ,lv_pernr(8) TYPE n ,t_photo type table of TBL1024 ,l_photo type XSTRING ,l_line type string ,l_photo1 type string ,ls_ret type BAPIRET2 ,ls_photo type TBL1024 ,t_msg type table of BAPIRET2. lv_entity_name = io_tech_request_context->GET_ENTITY_TYPE_NAME( ). CASE lv_entity_name. WHEN 'EmployeeData'. WHEN 'EmployeePhoto'. employeephotoset_get_entity( EXPORTING iv_entity_name = iv_entity_name iv_entity_set_name = iv_entity_set_name iv_source_name = iv_source_name it_key_tab = it_key_tab it_navigation_path = it_navigation_path io_tech_request_context = io_tech_request_context IMPORTING er_entity = employeephotoset_get_entity ). IF employeephotoset_get_entity IS NOT INITIAL. * Send specific entity data to the caller interface copy_data_to_ref( EXPORTING is_data = employeephotoset_get_entity CHANGING cr_data = er_entity ). ELSE. * In case of initial values - unbind the entity reference er_entity = lr_entity. ENDIF. lv_pernr = employeephotoset->get_entity-EmpID. CALL FUNCTION 'PAD_PHOTO_UPDATE_GET_DETAIL' EXPORTING IV_EMPLOYEE_NUMBER = lv_pernr TABLES T_PHOTO_ARCHIVE_OUT = t_photo T_MESSAGES_OUT = t_msg . LOOP AT t_photo into ls_photo. l_line = ls_photo-line. concatenate l_photo1 l_line into l_photo1. ENDLOOP. ls_stream-value = l_photo = l_photo1. ls_stream-mime_type = 'image/jpeg'. copy_data_to_ref( EXPORTING is_data = ls_stream CHANGING cr_data = er_stream ). WHEN OTHERS. ENDCASE.
Note that in order to retrieve attachment (photo, file), add $value to service [/getPhotoSet(EmpId=’1′)/$value]
Media Link in SAP GW
The following is an example of the source code for the software componentIW_BEPfor the redefined methodDEFINEof class/IWBEP/CL_MGW_ABS_MODELand/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM of class /IWBEP/CL_MGW_ABS_MODEL.
Redefined methodÂDEFINEÂ of classÂ/IWBEP/CL_MGW_ABS_MODEL to retrieve attachment in SAP GW
" setting a data object as media type means " that it gets a special semantic by having a url and allows streaming etc. lo_data_object->set_is_media( ). lo_property = lo_data_object->create_property( iv_property_name = 'mimeType' iv_abap_fieldname = 'MIME_TYPE' ). " must be set when data object is a media type " to mark the property which represents the mime type information lo_property->set_as_content_type( ).
/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM of class /IWBEP/CL_MGW_ABS_MODEL:
DATA: ls_stream TYPE ty_s_media_resource ,lo_api TYPE REF TO if_mr_api . lo_api = cl_mime_repository_api=>get_api( i_prefix = '/SAP/PUBLIC/BC/Pictograms' ). lo_api->get( EXPORTING i_url = '3_people_money.gif' IMPORTING e_content = ls_stream-value e_mime_type = ls_stream-mime_type ). copy_data_to_ref( EXPORTING is_data = ls_stream CHANGING cr_data = er_stream ).
Source:SAP Gateway Foundation (SAP_GWFND): Media Links
Going Further with SAP Gateway Odata Service
- a step by step on how to retrieve Employee’s Photo using SAP oData Service [help.sap]
- Simple Step-by-Step SAP-Gateway Service Guide
- SAP Gateway and OData (2nd Edition) [Book]
- OData and SAP Netweaver Gateway [Book]
- Learn how to use SAP Netweaver Gateway for UI5 and ABAP projects [Book]
- SAP Netweaver Gateway for SAPUI5, SAP Fiori and SAP HANA