Generic Object Services (Upload, Download,
List) Programatically
I recently found myself needing to write a web application that needed to interact with the attachments stored
in Generic Object Services within SAP. The Generic Object Services are used throughout multiple application
areas within SAP and have many features which include document (object) management. If you have ever
seen this icon
while using an SAP transaction, then you have the ability to use the Generic Object
Services in the particular business process. My goal was to be able to create and download the documents
from within my web application. In order to do this, I created some classes to help me. I will try to describe
them below.
Note: You will always need the business object key data regarding which object type you are working with
while coding for Generic Object Services. For example, if you were using Generic Object Services from
transaction PA30 (Human Resources), the object key would be BUS1065. If you were using Generic Object
Services in Orders (transaction IW33), the object key would be BUS2088. This key can easily be determined
by placing a break point in class CL_BINARY_RELATION and method READ_LINKS_OF_BINRELS prior to
executing the icon above. Or you can use transaction SWO1 to find Business Objects.
Getting Started: Create a Class to Build your Methods
First I created the following method: GOS_GET_FILE_LIST
This method allowed me to show a list of all files currently attached to the object I was working with. This
method could be a wrapper around standard function module: BDS_GOS_CONNECTIONS_GET. If you call
this method with your object key, you will retrieve a listing of all attachments. I chose to write the following
method code instead:
method gos_get_file_list.
types: begin of ts_key,
foltp type so_fol_tp,
folyr type so_fol_yr,
folno type so_fol_no,
objtp type so_obj_tp,
objyr type so_obj_yr,
objno type so_obj_no,
forwarder type so_usr_nam,
end of ts_key,
begin of ts_attachment,
foltp type so_fol_tp,
folyr type so_fol_yr,
folno type so_fol_no,
objtp type so_obj_tp,
objyr type so_obj_yr,
objno type so_obj_no,
brelguid type oblguid32,
roletype type oblroltype,
end of ts_attachment,
tt_attachment type table of ts_attachment.
data: ta_srgbtbrel type standard table of srgbtbrel,
wa_srgbtbrel type srgbtbrel,
lta_sood type standard table of sood,
lwa_sood type sood, ltp_pathin(1000) type c,
ltp_filename type string,
ltp_sortfield type char30,
lta_objcont type soli_tab,
lta_attachments type tt_attachment,
lwa_attachments like line of lta_attachments,
lo_boritem type ref to cl_sobl_bor_item,
lo_al_item type ref to cl_gos_al_item,
li_link type ref to if_browser_link,
ls_option type obl_s_relt,
lt_options type obl_t_relt,
ls_key type ts_key,
ls_attachment type ts_attachment,
lt_attachment type tt_attachment,
lt_links type obl_t_link,
ls_link type obl_s_link,
lp_linkid type blnk_inst,
gs_lpor type sibflporb.
if not objtype is initial and not objkey is initial.
select * from srgbtbrel into table ta_srgbtbrel
where instid_a eq objkey
and typeid_a eq objtype
and catid_a eq 'BO'.
if sy-subrc eq 0.
sort ta_srgbtbrel by instid_a typeid_a catid_a.
delete adjacent duplicates from ta_srgbtbrel comparing instid_a typeid_a catid_a.
loop at ta_srgbtbrel into wa_srgbtbrel.
clear: lt_attachment[], lta_attachments[].
gs_lpor-instid = wa_srgbtbrel-instid_a.
gs_lpor-typeid = wa_srgbtbrel-typeid_a.
gs_lpor-catid = wa_srgbtbrel-catid_a.
ls_option-sign = 'I'.
ls_option-option = 'EQ'.
ls_option-low = 'ATTA'.
append ls_option to lt_options.
ls_option-low = 'NOTE'.
append ls_option to lt_options.
ls_option-low = 'URL'.
append ls_option to lt_options.
try.
call method cl_binary_relation=>read_links_of_binrels
exporting
is_object
endif.
data rcode type i.
data objhead_tab type table of soli.
data objcont_tab type table of soli.
data objpara_tab type table of selc.
data objparb_tab type table of soop1.
data sood_key type soodk.
data hex_mode type sonv-flag.
field-symbols <fs> type line of z_tt_sood.
loop at t_attachments assigning <fs>.
if not ( <fs>-objtp is initial or <fs>-objyr is initial or <fs>-objno is initial ).
concatenate <fs>-objtp <fs>-objyr <fs>-objno into sood_key.
perform socx_select in program sapfsso0
tables objhead_tab objcont_tab
objpara_tab objparb_tab
using sood_key
hex_mode
rcode.
if rcode eq 0.
data moff type i.
data l_param_search type soli-line.
data l_param_head type soli-line.
data value type soli-line.
data wa_objhead_tab like line of objhead_tab.
data lt_url_tab type table of so_url.
data ld_url_tab_size type sytabix.
l_param_search = '&SO_FILENAME'.
translate l_param_search to upper case.
loop at objhead_tab into wa_objhead_tab.
clear moff.
find '=' in wa_objhead_tab-line match offset moff.
check sy-subrc = 0.
l_param_head = wa_objhead_tab-line(moff).
translate l_param_head to upper case.
if l_param_head = l_param_search.
add 1 to moff.
value = wa_objhead_tab-line+moff.
if not ( value is initial ).
split value at '.' into table lt_url_tab.
describe table lt_url_tab lines ld_url_tab_size.
if ld_url_tab_size gt 1.
read table lt_url_tab index ld_url_tab_size into <fs>-acnam.
endif.
endif.
endif.
endloop.
endif.
endif.
endloop.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code
= '001'
i_v1
= 'There are no Attachments on this Business Object'
report ztest_file_list.
data: ta_srgbtbrel type standard table of srgbtbrel, wa_srgbtbrel type srgbtbrel,
lta_sood type standard table of sood, lwa_sood type sood.
parameters: p_key type swo_typeid obligatory default '000030026122',
p_type type swo_objtyp obligatory default 'BUS2088'.
start-of-selection.
data: t_st_return type zvnt_st_return.
call method zcl_oh_my_gos=>gos_get_file_list
exporting
objtype
= p_type
objkey
= p_key
importing
t_attachments = lta_sood
changing
es_return = t_st_return.
if t_st_return-code is initial.
data dec_kb type p.
loop at lta_sood into lwa_sood.
dec_kb = lwa_sood-objlen / 1024.
if dec_kb < 1.
dec_kb = 1.
endif.
write: / lwa_sood-objdes, dec_kb, 'KB'.
endloop.
else.
message t_st_return-text type 'W'.
endif.
Second I created the following method: GOS_ATTACH_FILE
This method allowed me to attach files to the object I was working with.
method gos_attach_file.
data: ls_object type borident,
ls_obj_data type sood1,
ls_obj_id type soodk,
ls_fol_id type soodk,
ld_filename type string,
lt_url_tab type table of so_url,
ld_url_tab_size type sytabix,
ls_folmem_k type sofmk,
lv_ep_note type borident-objkey,
ls_note type borident.
data: it_objhead type standard table of soli.
ls_object-objtype = objtype.
ls_object-objkey = objkey.
if not ls_object-objtype is initial and not ls_object-objkey is initial.
call function 'SO_CONVERT_CONTENTS_BIN'
exporting
it_contents_bin = it_content[]
importing
et_contents_bin = it_content[].
if sy-subrc eq 0.
call function 'SO_FOLDER_ROOT_ID_GET'
exporting
region
receiving
rs_return = es_return.
endif.
endmethod.
Testing: Here is some sample code for a report program which will test the two methods above. It attaches a
file to the key object specified and shows the before and after results:
report ztest_file_attach.
parameters: p_key type swo_typeid obligatory default '000030026122',
p_type type swo_objtyp obligatory default 'BUS2088',
p_file type rlgrap-filename obligatory.
at selection-screen on value-request for p_file.
start-of-selection.
data: g_filename type string,
g_attsize type wsrm_error-wsrm_direction,
it_content like standard table of soli.
data: ta_srgbtbrel type standard table of srgbtbrel, wa_srgbtbrel type srgbtbrel,
lta_sood type standard table of sood, lwa_sood type sood.
data: t_st_return type zvnt_st_return.
data dec_kb type p.
* LIST
call method zcl_oh_my_gos=>gos_get_file_list
exporting
objtype
= p_type
objkey
= p_key
importing
t_attachments = lta_sood
changing
es_return = t_st_return.
if t_st_return-code is initial.
loop at lta_sood into lwa_sood.
dec_kb = lwa_sood-objlen / 1024.
if dec_kb < 1.
dec_kb = 1.
endif.
write: / lwa_sood-objdes, dec_kb, 'KB'.
endloop.
Lastly, I created the following method: GOS_DOWNLOAD_FILE
This method allowed me download an attachment from the object I was working with. For simplicity, I will show
how I downloaded the files from the GUI.
method gos_download_file.
data: ex type ref to cx_root, text type string.
data: ltp_sortfield type char30,
lta_objcont type soli_tab,
ltp_pathfile(1000) type c,
ltp_filename type string,
ltp_binfilesize type so_doc_len.
try .
concatenate attachment-objtp attachment-objyr attachment-objno into ltp_sortfield.
import objcont_tab to lta_objcont from database soc3(dt) id ltp_sortfield.
if sy-subrc = 0.
concatenate file_path '\' attachment-objdes '.' attachment-file_ext into ltp_pathfile.
replace '\\' with '\' into ltp_pathfile+2.
translate ltp_pathfile using '/ '.
ltp_binfilesize = attachment-objlen.
call function 'SO_OBJECT_DOWNLOAD'
exporting
bin_filesize
receiving
rs_return = es_return.
endif.
catch cx_root into ex.
text = ex->get_text( ).
call method zcl_oh_my_gos=>log_msg
exporting
i_code
i_v1
= '021'
= text
receiving
rs_return = es_return.
endtry.
endmethod.
Testing: Here is some sample code for a report program which loops through all attachments of an object and
downloads them.
report ztest_file_download.
data: ta_srgbtbrel type standard table of srgbtbrel, wa_srgbtbrel type srgbtbrel,
lta_sood type standard table of sood, lwa_sood type sood.