Skip to Content
Author's profile photo Rashmi BR

How to Read Photo from SAP system using SAP Gateway

Consider a scenario where we need to read  (1) a Business Object entry and also (2) an associated attachment (document, photo or video) uploaded in the SAP Business Suite system. Requirement(1) can be achieved by generating an OData service using Service Builder(SB)

In this blog, we shall focus on the implementation of requirement(2) and we make use of the extensibility feature of SB.

 

Reading of an attachment in OData world is in the form of a byte stream also called Media Links. Though not directly supported by SB, it has a provision to enable the same via an annotation called ‘Media’ at the EntityType level.

For easy understanding, let’s see a use case here….

__________________________________________________________________________________________________________

Use Case:

Let’s consider a scenario where an Employee photo has to be read in addition to Employee details from SAP Business Suite system using SAP NetWeaver Gateway.

Entities: EmployeeData and EmployeePhoto

EmployeeData EmployeePhoto
EmpID(k) EmpID(k)
Lastname ContentType
Firstname Uri
Title
MaritalStatus

____________________________________________________________________________________________________________________________

Prerequisites:

  1. Gateway IWBEP system: IW_BEP 200 SP5 and above
  2. Gateway IWFND system: IW_FND 250(Gateway HUB) SP3
  3. A suitable HCM SAP Business Suite System

Design Employee model with both the entities having default entity-sets created, linked via a bidirectional association having cardinality 1:1.

Following sample BAPIs have been used in this blog.

  1. BAPI_EMPLOYEE_GETDATA: To get the Employee Data.
  2. HRWPC_RFC_EP_READ_PHOTO_URI: To get URI of the photo of an Employee uploaded in the SAP Business Suite system.
  3. PAD_PHOTO_UPDATE_GET_DETAIL: To get byte stream of the Employee’s photo to be rendered.

Please ensure these/equivalent BAPIs/BORs are available in the SAP Business Suite system that will be used in your implementation.

_____________________________________________________________________________________________________________

Let’s take this ahead with a step by step approach :-

  1. Tx code: SEGW

   2.  Set the annotation ‘Media’ of ‘EmployeePhoto’  entity

MediaLink.JPG or the

        Generate Runtime Context Menu option on Project node.

        The Runtime Artifacts get generated successfully with the message  /wp-content/uploads/2013/02/runtimemsg_188175.pngon the status bar.


  4.   Redefine the DEFINE method of Extended Model Provider class ZCL_ZMEDIALNK_SB_MPC_EXT .

            

    Redefine_MPC1.png    

    Redefine_MPC2.png

       Select the method and click on Redefine button as shown in the above screen shot.

       Double click on the method to open the editor.

      Copy and Paste the Code snippet from this link.

Click on Save and Activate the class.

 

5.     Go to Service Implementation folder. Expand the EmployeeDataSet node and select            GetEntity(Read).

        Right click and select the option Map to Data Source.

    • Here we should use an RFC/BOR to get the Personal Data of EmployeeData record

                for example: ‘BAPI_EMPLOYEE_GETDATA’

      Map_ToDS1.png Map_ToDS2.png

       Mapping: Drag and drop EMPLOYEE_ID of the Data Source(DS) Parameters to EmpID property

       of the Entity Set. To map the other properties, expand the Table Type PERSONAL_DATA[] and

       drag-drop the DS parameter to the corresponding Entity Set properties as shown in the screen

       shot below.

     Map_ToDS3.png

 

6.   Go to Service Implementation folder. On similar lines, expand the EmployeePhotoSet node and

      select GetEntity(Read).

       Right click and select the option Map to Data Source.

    • Here we should use an RFC/BOR to get the Uri and ContentType of EmployeePhoto record

               for example: ‘HRWPC_RFC_EP_READ_PHOTO_URI’


    Map_ToDS2_photo.png

   Photo_mapping.JPG


7.   Generate the Runtime Artifacts.

      Mapping DS in step 5 and 6 results in generation of Data Provider implementations of

      GetEntity (Read) for ‘EmployeeData’ and ‘EmployeePhoto’ entitysets.

     

      This enables reading EmployeeData and EmployeePhoto records.

      for example:

The above URL would fetch the EmployeeData with EmpID = 00100276

empData_set2.jpg

The above URL would fetch the EmployeePhoto with EmpID = 00100276

/wp-content/uploads/2013/02/empphoto_set1_188248.jpg

Here Uri depicts the link to the SAP Business Suite System repository where the media/photo actually resides.

8.   This bring us to the final step of rendering the EmployeePhoto via the IWBEP system.

      To achieve this we need to use a BAPI/BOR which can take EmpID as input and return the byte stream of the photo.

      In our example we use the BAPI ‘PAD_PHOTO_UPDATE_GET_DETAIL’

     Here we need to redefine the /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM method of the

       Extended Data Provider Class  ZCL_ZMEDIALNK_SB_DPC_EXT.

         DPC_redefine1.png

      On similar lines of redefintion of DEFINE method in Extended Model Provider Class,

      here the /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM method of Extended Data Provider class

      needs to be redefined.

DPC_redefine2.png

    Double click on the method to open the editor. Copy and paste the code snippet found here.

    Click on Save and Activate the class.

     Now the SAP NetWeaver Gateway service generated and extended for our Business requirement

     in this SB Project is ready for consumption.

 

     To read the media link, Employee photo in our case, we need to suffix /$value to the GetEntity(Read)

    URL of EmployeePhoto i.e.,

 

     http://server/sap/opu/odata/sap/ZMEDIALINK_SB_SRV/EmployeePhotoSet(‘00100276’)/$value

   

     And we are DONE!!! Hope this makes Reading Media Links via SB easier for you.

 

Assigned Tags

      42 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Syam Babu
      Syam Babu

      Excellent Blog.

      How much multimedia data size we can import ?

      Is it possible to develop with SP4 system?

      Thanks,

      Syam

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Thanks a lot Syam. Media types are supported in SP05 and above landscapes. Regarding the data size, will check and get back to you!

      Best Regards,

      Rashmi

      Author's profile photo Former Member
      Former Member

      Hi Rashmi,

      In the simialr way, i created zfunction module with

      input

      employee number, sequence number

      output

      payslip  in xstring format

      pdf_fsize as i.

      now i need to show this xstring output in odata services.

      step1:

      i created project in segw transaction .

      created entity type

      can you help me after that , how to incorporate..

      Author's profile photo Syam Babu
      Syam Babu

      Hi Vijay,

      As suggested Rashmi,you can follow the same steps in SEGW.

      If you want develop normal way you can follow Arun's Blog.

      Thanks,

      Syam

      Author's profile photo Former Member
      Former Member

      Thanks for the reply syam,

      i followed the steps which are mentioned by rashmi, after creating entity type as media, is it mandatory to implement get_stream.if yes what i need to incorporate...

      here i created only one entity type as media and in getentity i used my custom rfc..

      thanks,

      Vijay

      Thanks,

      Vijay

      Author's profile photo Syam Babu
      Syam Babu

      Hi Vijay,

      Yes..you could implement code in that method.

      http://scn.sap.com/docs/DOC-36428

      Thanks,

      Syam

      Author's profile photo S. Vikgnesh
      S. Vikgnesh

      Hi Rashmi,

      Great blog!

      I followed this blog and was able to read the photo and see the photo in Gateway client after executing GET operation,

      I used the below code to read the photo in SAPUI5 application.

        var sRead = "/getPhotoSet(EmpId='1')/$value";

        oModel.read(sRead, null, null, true, function(oData, response){

        var oImage = new sap.ui.commons.Image({

        id : "image1", // sap.ui.core.ID

        src : response.requestUri,

        width : "200px"

        });

        }

      But this is again triggering server roundtrip. In the response tab, i have binary data.

      How to use the response (binary data) with the image control?

      for example, i tried putting data:image/jpeg;base64," + response.results instead of response.requestUri but the image is not shown.

      Please guide.

      Regards,

      S.Vikgnesh

      Author's profile photo Steven De Saeger
      Steven De Saeger

      Great blog ! Just what I was looking for ... I wanted to start adding media to my existing entity sets ...

      I was wondering ... it would probably also make sense to create an 'association' between the 2 entity sets no ?

      Steven

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Thanks a lot Steven. It depends on the model, in the above example since the photo(media) is maintained in another entity, an association was necessary to navigate to the details! Please let me know the scenario you are working on if you still require any help...

      Best Regards,

      Rashmi

      Author's profile photo Aliaksandr Shchurko
      Aliaksandr Shchurko

      Thanks!

      Author's profile photo Former Member
      Former Member

      Thanks for sharing it is very much informative.

      Author's profile photo Arun Chembra
      Arun Chembra

      Hi Rashmi,

      Thanks for sharing this document, but am not using SEGW for service creation. I am creating Model Provider Class and Data Provider Class Manually. In model provider class i am facing some issue so can you share the Model provider class code ....

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Hi Arun,

      Thanks a lot for reading thru the blog. w.r.t the Model Provider class, I have shared the handwritten media link specific code at this link. Kindly let me know if you require any further help.

      Thanks,

      Rashmi

      Author's profile photo Arun Chembra
      Arun Chembra

      Hi Rashmi,

      I have used the same logic inside Model Provider Class but while maintain model in SPRO i am getting error message saying "Data Oobject 'EmployeePhoto' Not found''.

      I am able to execute my Data Provider Class in se24, facing issue only in model provider class. The below code only i have placed in my model provider...

      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.

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Hi Arun,

      Can you share your metadata document? will check and let you know

      Thanks,

      Rashmi

      Author's profile photo Arun Chembra
      Arun Chembra

      Hi Reshmi,

      Thanks for your reply....

      I just paste your code inside Data provider class and Model provider class.

      Apart from that i have add anything else?

      In model provider class i am getting error, metadata documents means model provider class only rite?

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Hi Arun,

      Metadata document gives the details of the service. All I need is your business model details, so that I can let you know which property is to be set as CONTENT_TYPE for the business object in your implementation

      ex of a metadata document : http://services.odata.org/Northwind/Northwind.svc/$metadata

      Thanks,

      Rashmi

      Author's profile photo Arun Chembra
      Arun Chembra

      Hi Rashmi,

      Thanks for your support...

      The issue got solved problem was with my model provider class only .

      Author's profile photo Roy Fishman
      Roy Fishman

      Excellent article!

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hello Rashmi,

      Really a nice and useful blog you have shared.

      Here are my clarifications.

      1.Does the same apply to attachment like 'PDF' also. My scenario is when the user from the device hits "Download as a PDF' button i want to generate a PDF(using SMARTFROMS) in NW Gateway and push them back to device to view the attachement.

      Bare me if my questions are simple as i am a newbie.

      Regards

      Prabaharan

      Author's profile photo Rashmi BR
      Rashmi BR
      Blog Post Author

      Hi Prabaharan,

      Thanks for going through my blog 🙂 . Yes, it is applicable to pdf (treated as one of the media types) as well. GET_STREAM has to be invoked on 'Download as a PDF' and continue with the custom development required.

      Best Regards,

      Rashmi

      Author's profile photo Former Member
      Former Member

      Hi Rashmi,

      Thanks for your useful blog..

      I have a requirement to display a payslip through service builder..can u pls guide me..

      Can we display a PDFstring as output in service builder.

      Regards,

      Vijay

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hi all,

      I have successfully uploaded and  stored documents like pdf ,images,ms word and ms excet in sap custom table. Now if I expose them as gateway services images and pdf are getting rendered in browser itself. However I couldn't display word and excel documents. Do anyone have idea on this? Any help is appreciated

      Regards

      Prabaharan

      Author's profile photo Former Member
      Former Member

      Hi Prabaharan,

      First of all i would like thank you for your wonderful blog.

      I have created in FM, Service in NW GW and UI Screen at SAP UI5 with respect to your blog.

      I got below response for my service.

      pdf url.PNG

      pdf.PNG

      CONCATENATE  '/sap/public' '/' lv_guid '.' 'PDF' INTO e_url.

      Function module is returning url like

      URL =  /sap/public/46C64CAC93CB1ED3B2BF04D15B23D801.PDF

      Capture pdf.PNG

      I have consumed above service or url in my ui5 application.

      Capture pdf2.PNG

      I got below error while running UI5 application.

      Capture pdf3.PNG

      Can you help on this? Where i done the mistake?

      Do i need to pass PDF converted string to UI5 as odata?

      Thanks in advance.

      Akshath

      Author's profile photo Former Member
      Former Member

      Hi Rashmi,

      Thanks for your blog. Its really very useful. I have pasted the Code snippet you have given for the GET_STREAM method. I am getting an error at the below line

      employeephotoset_get_entity TYPE ZCL_ZMEDIALNK_SB_MPC=>TS_EMPLOYEEPHOTO


      The error was :ZCL_ZMEDIALNK_SB_MPC is unknown. I have checked the class ZCL_ZMEDIALNK_SB_MPC. It is in active state. The service is also active. But when I double click on the above type I was not navigated.


      I created the service through SEGW transaction itself and generated the Runtime artifacts.


      Could you please help me on this? or did I miss anything?

      Author's profile photo Syam Babu
      Syam Babu

      Hi Sowjanya,

      cross verify your class names and entity set once again.

      Thanks,

      Syam

      Author's profile photo Arun Chembra
      Arun Chembra

      Hi Sowjanya,



      Please check the below points


      1. In se24 open class ZCL_ZMEDIALNK_SB_MPC,click on tab Types and verify TS_EMPLOYEEPHOTO declared or not.

      2. How you are redefine GET_STREAM method? You have to redeine method GET_STREAM inside class ZCL_ZMEDIALNK_SB_DPC_EXT.

      3. If you want to try Old method using ABAP coding you can follow the below link Gateway Service using the OData Channel Approach (ABAP Coding)  for Media Links(Photo,Document)


      Thanks,

      Arun

      Author's profile photo Former Member
      Former Member

      Hi Arun,

      Nice explanation...

      Thanks a lot...

      Author's profile photo Former Member
      Former Member

      HI,

      How can we upload those file in MIME repository ?

      My requirement is ==>

      I have upload file in SAP-UI5, those file should upload in MIME repository (SAP-ECC)back end.

      How can i do this? 

      I have referred below link (which is shown  Gateway connection details).

      http://scn.sap.com/community/netweaver-gateway/blog/2013/08/14/how-to-upload-and-download-files-using-sap-nw-gateway#comment-470346

      Kindly help me on this...

      Author's profile photo Former Member
      Former Member

      Hello rashmi,

      I was trying out your code to map my requirements.  i get an error with the below code

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


      i used my Structure instead of the below statements

      Data: employeephotoset_get_entity TYPE ZCL_ZMEDIALNK_SB_MPC=>TS_EMPLOYEEPHOTO

      Author's profile photo Prabaharan Asokan
      Prabaharan Asokan

      Hi Rashmi,

      I am trying to download a file using ECC as a source system via SAP Netweaver gateway OData service. I created a Entity type and marked as "Media = X' in service builder. But when i tried to redefine the method method using below code snippet

      method DEFINE.

      DATA: lo_entity_type TYPE REF TO /iwbep/if_mgw_odata_entity_

      typ,

      lo_property TYPE REF TO /iwbep/if_mgw_odata_property.

      super->define( ).

      lo_entity_type = model->get_entity_type( 'Product' ).

      lo_entity_type->set_is_media( ).

      lo_property = lo_entity_type->get_property( 'PictureURI' ).

      lo_property->set_as_content_source( ).

      lo_property = lo_entity_type->get_

      property( 'PictureMIMEType' ).

      lo_property->set_as_content_type( ).

      endmethod.

      Upon syntax check i am getting error as "set_as_content_source is unknown or protected" because i couldn't find above highlighted methods in the class "/IWBEP/IF_MGW_ODATA_PROPERTY"(SP5). However i could find the above methods in SAP NW Gateway demo system(SP6).

      Please suggest.

      Regards

      Prabaharan

      Author's profile photo Deborah Castle
      Deborah Castle

      Thanks so much!  I adapted this and am able to display a pdf!

      Author's profile photo S. Vikgnesh
      S. Vikgnesh

      Hi Deborah,

      Can you please share the code used in UI5 application for binding the binary data to your control? I am able to display pdf in gateway client but i am facing trouble for displaying in UI5. Should the response.requestUri be used in src property or the binary data retrieved can be used ?

      Regards,

      S.Vikgnesh

      Author's profile photo S. Vikgnesh
      S. Vikgnesh

      Hi Rashmi,

      To retrieve multiple photos for example 2 photos, do we need to call the oModel.read() two times.

      http://server/sap/opu/odata/sap/ZMEDIALINK_SB_SRV/EmployeePhotoSet('00100276')/$value

      http://server/sap/opu/odata/sap/ZMEDIALINK_SB_SRV/EmployeePhotoSet('00100277')/$value

      Any better approach?

      Regards,

      S.Vikgnesh

      Author's profile photo Shakeeluddin Khaja
      Shakeeluddin Khaja

      Useful Info . Thanks Rashmi

      Author's profile photo Siva prasad Ramani
      Siva prasad Ramani

      Upload photo in gateway service SAP.

      Hi.

      I have two systems. One is CRM for data retrieving, another is GWD for create and maintain Gateway service. I did write code in CRM system for Photo upload(RFC). That called into gateway service system. that hole code wrote in update method.

      when i execute that method it displayed bad request http response.

      please help me...

      /wp-content/uploads/2014/12/gwd_606971.png

      Author's profile photo Ashwin Dutt R
      Ashwin Dutt R

      Hello Siva,

      Please correct the URL.

      You need to set Entity as Media Type.

      You need to Implement Create_Stream method in your DPC_EXT class and inside that method you need to call your logic which will save your media content in back-end system.

      You need to pass Key in a Header called ' slug ' and this has to be accessed inside your Create_Stream Method.

      You need to pass a Header called Content-Type which will specify the Mime Type

      Example -> if you are Uploading a .pdf file then you need to pass

      Content-Type : application/pdf

      The HTTP Method should be POST when you are Creating Entry newly.

      Demo URL for your Reference ->

      Post_Image.png

      Refer for more info ->

      Uploading Files to SAP GW, Downloading Files from SAP GW - New Techniques

      Upload Image to SAP Gateway and Display Image in UI5 - Using New Fileuploader with SAP Gateway

      How To Upload and Download Files Using SAP NW Gateway SP06

      Regards,

      Ashwin

      Author's profile photo Siva prasad Ramani
      Siva prasad Ramani

      Thank you for your response Ashwin.

      yeah I corrected my URI as you said.

      This entity set is for Update not create.

      And I already implemented streams methods in

      my DPC_EXT method  in class, I passed Image content, mime type and etc...

      No problem at all in stream methods and passed contents.

      Below screen shot is my entity set structure.

      /wp-content/uploads/2014/12/gwd3_607042.png

      Here.. my problem is when i execute put method it says 'Error while parsing an XML stream'.

      /wp-content/uploads/2014/12/gwd2_607049.png

      there is wrong in XML code i guess, please correct my XML code & help me..

      Regards,

      Siva.

      Author's profile photo Ashwin Dutt R
      Ashwin Dutt R

      Hello Siva,

      In the payload you are sending value of the media content through mimeSrc property.

      But looking at the model level it is defined as Edm.string.

      The property which is holding the media content value will be of type Edm.Binary.

      According to me it Should not be Edm.String. Please check this point.

      In the GW Model Property mimeType is defined with some precision which is not correct.

      Remove that and make Max Length say 100.

      Please check the above points.

      I am not sure why you are using an XML payload to update stream which contains your media content value inside the property mimeSrc.

      When you say you want Update a Media Content it will be a new content which you want to update for a particular entry ( Key Value ) all together correct ?

      For Example in back-end my old photo is saved at the time of joining my new company.

      Now after 5 yrs HR dept asks me my Recent Photo. Now they attach my new Photo and fire an Update Stream by passing my employee ID correct ? So that its is Updated to the latest one.

      so why cant you attach the new content as part of attachment and fire the Update Stream by passing only the key and the mime type of the new content ?

      I think XML payload is not required.

      I do not know your use case but just shared my thoughts 🙂

      Now coming to the way you need to fire an Stream Update is as follows :


      Implement /IWBEP/IF_MGW_APPL_SRV_RUNTIME~UPDATE_STREAM method with the necessary Code.


      XML payload :


      /wp-content/uploads/2014/12/update_stream_xml_payload_607186.png


      When its a New attachment and you need to fire an Update for a particular entry :



      /wp-content/uploads/2014/12/new_attachment_update_stream_607190.png

      Just to add the below to Delete the stream :


      Implement /IWBEP/IF_MGW_APPL_SRV_RUNTIME~DELETE_STREAM.


      Pass the Key.


      /wp-content/uploads/2014/12/delete_stream_607191.png

      Regards,

      Ashwin

      Author's profile photo Siva prasad Ramani
      Siva prasad Ramani

      Hi all,

      I'm struggling with photo upload in SAP gateway service.

      Could you please provide me step by step procedure of photo upload in SAP gateway service.

      Regards,

      Siva R.

      Author's profile photo Former Member
      Former Member

      Hi Siva,

      Please check whether one of my Past queries is helpful to you.

      "Invalid or no Mapping to System Data Types Found" while using the CREATE_STREAM

      Thanks and Regards,

      Kartik Suru

      Author's profile photo Aditya Agarwal
      Aditya Agarwal

      I have used the same logic inside Model Provider Class but while maintain model in SPRO i am getting error message saying “Data Oobject ‘EmployeePhoto’ Not found”.

      I am able to execute my Data Provider Class in se24, facing issue only in model provider class.

      1. This error comes if we try to copy the above code and paste directly then the code  in GET_PROPERTY( IV_PROPERTY_NAME = ‘ContentType’  ( in inverted code are not copied properly and hence they do not get declared . Once this is solved you will get another error. You need to fix this for all inverted comma used codes.
      2. The field “EMPLOYEEPHOTOSET_GET_ENTITY–EMPID” is unknown, but there is  a field with the similar name “EMPLOYEEPHOTOSET_GET_ENTITY”.