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:
- Gateway IWBEP system: IW_BEP 200 SP5 and above
- Gateway IWFND system: IW_FND 250(Gateway HUB) SP3
- 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.
- BAPI_EMPLOYEE_GETDATA: To get the Employee Data.
- HRWPC_RFC_EP_READ_PHOTO_URI: To get URI of the photo of an Employee uploaded in the SAP Business Suite system.
- 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 :-
- Tx code: SEGW
-
- Create a Gateway project: ZMEDIALNK_SB
- Import the designed metadata file using File Import functionality
2. Set the annotation ‘Media’ of ‘EmployeePhoto’ entity
Generate Runtime Context Menu option on Project node.
The Runtime Artifacts get generated successfully with the message on the status bar.
4. Redefine the DEFINE method of Extended Model Provider class ZCL_ZMEDIALNK_SB_MPC_EXT .
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’
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.
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’
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
The above URL would fetch the EmployeePhoto with EmpID = 00100276
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.
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.
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.
Excellent Blog.
How much multimedia data size we can import ?
Is it possible to develop with SP4 system?
Thanks,
Syam
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
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..
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
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
Hi Vijay,
Yes..you could implement code in that method.
http://scn.sap.com/docs/DOC-36428
Thanks,
Syam
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
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
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
Thanks!
Thanks for sharing it is very much informative.
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 ....
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
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.
Hi Arun,
Can you share your metadata document? will check and let you know
Thanks,
Rashmi
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?
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
Hi Rashmi,
Thanks for your support...
The issue got solved problem was with my model provider class only .
Excellent article!
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
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
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
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
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.
CONCATENATE '/sap/public' '/' lv_guid '.' 'PDF' INTO e_url.
Function module is returning url like
URL = /sap/public/46C64CAC93CB1ED3B2BF04D15B23D801.PDF
I have consumed above service or url in my ui5 application.
I got below error while running UI5 application.
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
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?
Hi Sowjanya,
cross verify your class names and entity set once again.
Thanks,
Syam
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
Hi Arun,
Nice explanation...
Thanks a lot...
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...
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
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
Thanks so much! I adapted this and am able to display a pdf!
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
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
Useful Info . Thanks Rashmi
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...
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 ->
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
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.
Here.. my problem is when i execute put method it says 'Error while parsing an XML stream'.
there is wrong in XML code i guess, please correct my XML code & help me..
Regards,
Siva.
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 :
When its a New attachment and you need to fire an Update for a particular entry :
Just to add the below to Delete the stream :
Implement /IWBEP/IF_MGW_APPL_SRV_RUNTIME~DELETE_STREAM.
Pass the Key.
Regards,
Ashwin
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.
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
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.