SAP4TECH.NET

SAP and ABAP Free Tutorials
Menu
  • SAP4TECH
    • Contact Us
    • Privacy and Cookie Policy
    • Sitemap
    • About Me
  • SAP ECC
    • SAP FI
    • SAP FSCM
    • SAP HR
    • SAP SD & SAP MM
    • SAP PM
    • SAP PP
    • SAP PS
    • SAP QM
    • SAP VIM
    • SAP WM
  • SAP CRM & SRM
    • SAP CRM
    • SAP SRM
  • SAP HANA
  • SAP BW
  • SAP Workflow
  • SAP Netweaver
    • ABAP Code Snippets
    • ABAP WebDynPro
    • SAP Fiori
    • SAP GW
    • SAP IDOC (ALE)
    • SAP PI (XI)
    • SAP Screen Personas

SAP GW Serie: Retrieve Attachment with GW (GET_STREAM )

siva125 July 28, 2017

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

  • Signature of GET_STREAM
  • /IWBEP/ Prerequis for GET_STREAM
    • Most common Method GET_STREAM Sample Implementations
    • Get Employee’s Photo using SAP GW
  • Media Link in SAP GW
  • Going Further with SAP Gateway Odata Service

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 matadata 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)

SET SAP GW EntityType to Stream
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
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 an attachment (file or picture) using SAP GW oData Service.

Get Employee’s Photo using SAP GW

Maybe the most famous scenario for GET_STREAM method is retrieving Photo of SAP Employee.
Here the implementation of GET_STREAM for EmployeePhoto : (source)

GET_STREAM : Get Employee's Photo using SAP GW oData service
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 read the 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 component IW_BEP for the redefined method DEFINE of class/IWBEP/CL_MGW_ABS_MODEL and /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:

Redefined method DEFINE of class /IWBEP/CL_MGW_ABS_MODEL:
ABAP
1
2
3
4
5
6
7
8
9
10
11
  " 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:

/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM of class /IWBEP/CL_MGW_ABS_MODEL:
ABAP
1
2
3
4
5
6
7
8
9
10
11
       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 HANASAP Netweaver Gateway for SAPUI5, SAP Fiori and SAP HANA

You may also like
Sponsored Links
Share
Tweet
Google+
Linkedin
Stumble
Email
Prev Article

Related Articles

SAP Netweaver Gateway Tcodes; SAP GW Transaction Codes
SAP Netweaver Gateway Tcodes regroups the most important SAP Gateway …

SAP GW Serie: Retrieve Attachment with GW (GET_STREAM )

SAP Netweaver Gateway GW Troubleshooting Guide, Find String in any SAP ABAP Code source
This GW Troubleshooting Guide for SAP Netweaver Gateway will guide …

SAP GW Serie: Retrieve Attachment with GW (GET_STREAM )

About The Author

siva125

Leave a Reply

Cancel reply

Search for

Recent Posts

  • SAP Online Text Repository (OTR): SAP OTR Tcodes, Tables, Function and Translation
  • The most used SAP WM BAPI, Function Modules and User-Exits
  • List of The most important JSON ABAP Classes in SAP
  • Main SAP PA Tcodes (Personal Administration Transaction Codes)
  • How to retrieve Mime Type of File in ABAP ? ( SAP Mime Types Tables)

Top SAP Books

SAP Fiori Implementation and Development
Advanced, Modern ABAP 7.5
ABAP to the Future: Advanced, Modern ABAP 7.5 (2nd Edition)
SAP Gateway and OData
SAP Gateway and OData (2nd Edition)
SAPUI5: Guide to UI5
SAPUI5: The Comprehensive Guide to UI5
ABAP Development for SAP HANA
ABAP Development for SAP HANA (2nd Edition)

SAP4TECH.NET

SAP and ABAP Free Tutorials

Recent Posts

  • SAP Online Text Repository (OTR): SAP OTR Tcodes, Tables, Function and Translation
  • The most used SAP WM BAPI, Function Modules and User-Exits
  • List of The most important JSON ABAP Classes in SAP
  • Main SAP PA Tcodes (Personal Administration Transaction Codes)
  • How to retrieve Mime Type of File in ABAP ? ( SAP Mime Types Tables)
  • Payment Request in SAP: Payment Request Tables, BAPIs and BADI.
  • List of Important SAP Material Types in SAP MM

Recent Posts

  • SAP Online Text Repository (OTR): SAP OTR …
  • The most used SAP WM BAPI, Function …
  • List of The most important JSON ABAP …
  • Main SAP PA Tcodes (Personal Administration Transaction …
  • How to retrieve Mime Type of File …
Copyright © 2018 SAP4TECH.NET
SAP and SAP logo are registered trademarks of SAP AG. sap4tech.netis not associated with SAP AG. We have made every effort to make sure provide relevant search results, use the content on this site at your own risk.

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok