How to store a mobile made picture in SAPoffice
Hi everybody,
in this blog post I’m going to show you how you are able to make a picture via a mobile application and store it as an attachment in SAP (SAPoffice). The blog post contains out of the following steps:
- General information
- Finding the storage place
- Encode and decode
- Creating the attachment
- Creating the linking
So let’s start:
1. General information
SAPoffice is a mailing and also a filing system with which you are able to store and administrate files but also able to send files inside the SAP system or over the internet. I’ll show you how you can make it to store a picture which was made with you mobile camera and save it as an attachment in the SAPoffice. The picture which is made via the phone’s own camera is going to be sent as a base64 encoded String to the SAP system. Some of you may ask themselves, why I receive the image encoded as a base64 String. In the background there is a ICF-Service which handles HTTP-Requests. This request is also responsible for receiving the picture. The image itself is encoded at the mobile application and sent via POST-Request to the SAP system. This is the main reason why I have to deal with a String.
2. Finding the storage place
It’s quite important for the SAP system where it hase to save the image. Therefore a function module called “SO_FOLDER_ROOT_ID_GET’ is existing:
CALL FUNCTION 'SO_FOLDER_ROOT_ID_GET'
EXPORTING
region = 'B'
IMPORTING
folder_id = lf_folder.
This FM returns the unique ID of the folder associated to the region which you set as an importing parameter.
3. Encode and decode
As already mentioned, the image is transfered to the SAP system as a base64 encoded String. For the further process this String is going to be converted into a XString. What is the difference? Both, String and XString are datatypes for objects with variable length. The only difference is that a String is for a sequence of text and the XString for a sequence of byte. The main conversion is done by a FM too. This one is called “SSCF_BASE64_DECODE”:
CALL FUNCTION 'SSFC_BASE64_DECODE'
EXPORTING
b64data = is_meldung-pic1
IMPORTING
bindata = lf_picture.
Internal, SAP stores all attachments as binary encoded files. This means that we have to get our XString into a binary form. This is done via the FM “SCMS_XSTRING_TO_BINARY”:
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lf_picture
append_to_table = 'X'
IMPORTING
output_length = lf_size
TABLES
binary_tab = lt_content.
As a return value you get a table which contains the whole content of the picture encoded and the length of the encoded value itself.
4. Creating the attachment
The first step is to create an object of type “SODOCCHGI1”. With this object you are able to provide general information like the description, language, size, datatype of the attachment and whether the file should be changeable or not for example. For creating the file you have to call the FM “SO_DOCUMENT_INSERT_API1”:
CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'
EXPORTING
folder_id = lf_folderid
document_data = ls_document_data
document_type = lf_doctype
IMPORTING
document_info = lf_docinfo
TABLES
CONTENTS_HEX = lt_content.
As importing parameters the FM takes the determined ID of the folder where the attachment should be stored, the object of type “SODOCCHGI1”, the datatype of the object and the binary encoded table which stores the image itself. The return value is an object of type “SOFOLENTI1”. This object contains the information of the unique created SAPoffice object. The last step is now to create the linking between the SAPoffice object and the SAP object itself.
5. Creating the linking
For making the correct linking between the two objects. For each of them you have to provide the following information:
- instid
This is nothing else then the unique number of the object.
- typeid:
This is the so called business object. Each object which can be created or can be stored through an application in SAP is a business object. A Notification for example has the ID 2038 –> “BUS2038”.
- catid:
Nothing else than a short ID which stands for the category. In our case we’re dealing with business objects –> “BO”.
Finally we can now start with creating the link. The class “cl_binary_relation” provides a method called “create_link”. This method takes the two created objects and builds as a relation type “ATTACHMENT” the linking. The complete linking procedure looks like following:
lo_object_a-instid = af_qmnum.
lo_object_a-typeid = 'BUS2038'.
lo_object_a-catid = 'BO'.
lo_object_b-instid = lf_att_key.
lo_object_b-typeid = 'MESSAGE'.
lo_object_b-catid = 'BO'.
TRY.
CALL METHOD cl_binary_relation=>create_link
EXPORTING
is_object_a = lo_object_a
is_object_b = lo_object_b
ip_reltype = 'ATTA'.
ENDTRY.
Now you picture should be attached in the attachment list for the corresponding SAP object.
For further questions just contact me!
Greetings
Stefan
Hello Stefan,
Great blog. I exactly have the same scenario of sending Encoded image to SAP and attach them into PDF. I tried to decode and display it in Smartfoms but no luck.
Can you post the code,how you retrieved the Encoded image within SAP and sent it via mail?
Your help is highly appreciated.
Regards
Prabaharan
Hi Prabaharan,
the received image is saved as a normal base64 encoded string. This means ... At the beginning I have a normal javascript function which encodes my image into a base64 String. Afterwards I make a request and whithin this request I just send the image (String) to the SAP System. I Store it there as a normal String and then I start with transforming it into binary data etc.
Don't hesitate to ask if you have further questions
Greetings
Stef
Stefan,
I send base64 encoded string like "data:Img/base64 aasz...." which is called as data URI. Now I transmitted this to SAP using jQuery Ajax POST.
Now if I want to embed this to Adobe form PDF, should we decode then convert to binary?
Basically I am trying to recreate the image I sent in SAP.how to achieve this?
Regards
Prabaharan
Hi again,
well you don't have to send a string which looks like "data:img/base64...". You only have to send the base64 String. What do you want with this String in SAP. Do you want to store it as an attachment for a BO? And this BO should be used then for creating a PDF?
Greetings
Absolutely brilliant blog post! Quick question on use, if I wanted to used this with a C# RFC call to retrieve the document attachment, is it better to pass the object link, or is there a process to pass the document contents? Or, do you have to repeat the decode/encode with the RFC call?
Regards,
James
Hi James,
I for my part made it with the decode / encode methode. A had then a base64 String and encoded it back so I was able to receive the picture.
Hope I could help 🙂
Greetings
Stef
Thank you, Stef.
I'm assuming you used the function SSFC_BASE64_ENCODE then? Or is there a better SAP function call?
I appreciate your knowledge and help.
Regards,
James
Hey James,
jep exactly! SSFC_BASE64_ENCODE was the function module 🙂
Hope I could help you 😉
Greetings
Hi Stefan,
I am also trying the same application. In the ABAP program, in step three, you had given two parameters, document data and document type. For an image from the camera, kindly let me know what I should use. Also, when linking pls do let me know how the data from the previous function module is used, since the variables are different.
Thanks a lot.
With regards,
Ashok
Hi Ashok,
first of all sorry for the late reply. Was a bit busy at the moment.
ls_document_data is a structure of type sodocchgi1. I'm using this for setting specific information and details about the document itself like the object name, object description, size etc. The document type in this case is simple 'JPG'.
When you want to link the information together I was using variables of type sibflporb. By using them I was able to set the relevant BUS-type and also set the right IDs for matching it.
Hope I could help you!
Greetings
Stefan
Hi Stephan,
I tried using this approach but i am getting error.
i create a FM which will accept a base64 and will convet into binary.
everything is fine till 'SO_DOCUMENT_INSERT_API1' .. this FM is giving subrc = 5 because it is not able to make and entry in SOFFCONT1.
Do i have to enable somethign to do this or anything is missing in my code?
Could you please help me in fixing this?
I created a discussion also for this issue.
upload image using GOS in IW53
Thanks
Pravin