0% found this document useful (0 votes)
472 views

SAP ABAP ODATA Code To Handle Attachments

The document describes a method to get file attachments from an SAP system. It loops through link objects to retrieve binary file content, converts it to an XSTRING, and encodes it to Base64 format to add to an attachment table along with metadata like file name and type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
472 views

SAP ABAP ODATA Code To Handle Attachments

The document describes a method to get file attachments from an SAP system. It loops through link objects to retrieve binary file content, converts it to an XSTRING, and encodes it to Base64 format to add to an attachment table along with metadata like file name and type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

METHOD get_file_content.

DATA:ls_attachments TYPE zst_ses_attach.

*------------------------------------------
*------------Get Links---------------------
*------------------------------------------

DATA : ls_object TYPE sibflporb,


lt_rel_options TYPE obl_t_relt,
ls_rel_options TYPE obl_s_relt,
lt_links TYPE obl_t_link,
ls_links TYPE obl_s_link.

ls_rel_options-low = 'ATTA'. "" Attachemnts


ls_rel_options-sign = 'I'.
ls_rel_options-option = 'EQ'.
APPEND ls_rel_options TO lt_rel_options.

ls_object-instid = im_ses_no. "SES No


ls_object-typeid = 'BUS2091'. "" PR
ls_object-catid = 'BO'. "" Business Object
REFRESH lt_links[].

TRY.
CALL METHOD cl_binary_relation=>read_links_of_binrels
EXPORTING
is_object = ls_object " Start object
it_relation_options = lt_rel_options " Link Types
ip_role = 'GOSAPPLOBJ' " Role type
IMPORTING
et_links = lt_links. " Table with Relationship
Records

CATCH cx_obl_parameter_error. " Incorrect Calling of Interface


CATCH cx_obl_internal_error. " Internal Error of Relationship Service
CATCH cx_obl_model_error. " Error with Model Roles
ENDTRY.

*------------------------------------------
*------------Get Content-------------------
*------------------------------------------

*Loop at links table to get the binary content of the attachments

DATA : lv_doc_id TYPE sofolenti1-doc_id,


lt_cont_bin TYPE TABLE OF solisti1,
lt_obj_header TYPE TABLE OF solisti1,
lt_cont_solix TYPE TABLE OF solix,
ls_doc_data TYPE sofolenti1.

LOOP AT lt_links INTO ls_links.


lv_doc_id = ls_links-instid_b.

REFRESH: lt_cont_bin[],
lt_cont_solix[].
CLEAR:ls_doc_data.
CALL FUNCTION 'SO_DOCUMENT_READ_API1'
EXPORTING
document_id = lv_doc_id
IMPORTING
document_data = ls_doc_data
TABLES
object_header = lt_obj_header
contents_hex = lt_cont_solix
object_content = lt_cont_bin
EXCEPTIONS
document_id_not_exist = 1
operation_no_authorization = 2
x_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

*To convert binary data to xstring using FM SCMS_BINARY_TO_XSTRING.


DATA: lv_lines TYPE char20,
lv_doc_size TYPE i,
lv_xstring TYPE xstring.

ls_attachments-lblni = im_ses_no.

TRY.

ls_attachments-file_name = lt_obj_header[ 1 ].
IF NOT ls_attachments-file_name IS INITIAL.
REPLACE ALL OCCURRENCES OF '&SO_FILENAME=' IN ls_attachments-file_name
WITH space.
CONDENSE ls_attachments-file_name.
ENDIF.

ls_attachments-file_type = lt_obj_header[ 2 ].
IF NOT ls_attachments-file_type IS INITIAL.
REPLACE ALL OCCURRENCES OF '&SO_FORMAT=' IN ls_attachments-file_type
WITH space.
CONDENSE ls_attachments-file_type.
ENDIF.

CATCH cx_sy_itab_line_not_found.

ENDTRY.
ls_attachments-file_id = lv_doc_id.

IF im_fileid IS NOT INITIAL.


IF ls_attachments-file_id EQ im_fileid.
"Do Nothing
ELSE.
CONTINUE. "Pass the loop
ENDIF.

ENDIF.

IF lt_cont_solix IS NOT INITIAL AND ls_doc_data-obj_type = 'TXT'.


lv_lines = lines( lt_cont_solix ) * 255.
SHIFT lv_lines LEFT DELETING LEADING ' '.
lv_doc_size = lv_lines.

"" Convert the binary data to Xstring


CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_doc_size
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_cont_solix
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ELSE. "Non TXT any other BIN format
CALL FUNCTION 'SO_SOLITAB_TO_SOLIXTAB'
EXPORTING
ip_solitab = lt_cont_bin
IMPORTING
ep_solixtab = lt_cont_solix.
*
* IF ls_doc_data-obj_type = 'TXT'.
lv_lines = lines( lt_cont_solix ) * 255.
SHIFT lv_lines LEFT DELETING LEADING ' '.
lv_doc_size = lv_lines.
* ENDIF.
*
"" Convert the binary data to Xstring
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_doc_size
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_cont_solix
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*
ENDIF.

IF im_content_flag EQ abap_true OR im_fileid IS NOT INITIAL.


"Content to Base64
DATA: ls_string_base64 TYPE string.
CLEAR ls_string_base64.
CALL FUNCTION 'SCMS_BASE64_ENCODE_STR'
EXPORTING
input = lv_xstring
IMPORTING
output = ls_string_base64.
ls_attachments-file_content = ls_string_base64.
ls_attachments-is_content = abap_true.
ENDIF.

CALL FUNCTION 'SDOK_MIMETYPE_GET'


EXPORTING
extension = ls_doc_data-obj_type
* X_USE_LOCAL_REGISTRY =
IMPORTING
mimetype = ls_attachments-mime_type.

ls_attachments-wi = im_wi.
APPEND ls_attachments TO rt_attachments .
CLEAR: ls_attachments, ls_doc_data .
ENDLOOP.
ENDMETHOD.

You might also like