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 = gs_lpor
it_relation_options = lt_options
ip_role = ‘GOSAPPLOBJ’
importing
et_links = lt_links.
loop at lt_links into ls_link.
case ls_link-typeid_b .
when ‘MESSAGE’.
ls_key = ls_link-instid_b.
move-corresponding ls_key to ls_attachment.
ls_attachment-roletype = ls_link-roletype_b.
if ls_link-brelguid is initial.
ls_attachment-brelguid = ls_link-relguidold.
else.
ls_attachment-brelguid = ls_link-brelguid.
endif.
append ls_attachment to lt_attachment.
when others.
continue.
endcase.
endloop.
catch cx_obl_parameter_error .
catch cx_obl_internal_error .
catch cx_obl_model_error .
catch cx_root.
endtry.
endloop.
lta_attachments[] = lt_attachment[].
check lines( lta_attachments ) > 0.
select * from sood into table lta_sood
for all entries in lta_attachments
where
objtp = lta_attachments-objtp and
objyr = lta_attachments-objyr and
objno = lta_attachments-objno.
if sy-subrc eq 0.
t_attachments[] = lta_sood.
es_return-text = ‘SUCCESS’.
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’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘No Business Object and/or Key Specified’
receiving
rs_return = es_return.
endif.
endmethod.
Testing This Method:
Here is some report source code to test the method via a program:
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 = folder_region
importing
folder_id = ls_fol_id
exceptions
others = 1.
if sy-subrc eq 0.
if not file_name_with_path is initial.
call function ‘SO_SPLIT_FILE_AND_PATH’
exporting
full_name = file_name_with_path
importing
stripped_name = ld_filename.
else.
call function ‘SO_SPLIT_FILE_AND_PATH’
exporting
full_name = file_name_no_path
importing
stripped_name = ld_filename.
endif.
if sy-subrc eq 0.
split ld_filename 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 ls_obj_data-file_ext.
read table lt_url_tab index 1 into ls_obj_data-objdes.
else.
clear ls_obj_data-file_ext.
ls_obj_data-objdes = ld_filename.
endif.
ls_obj_data-objsns = ‘O’.
ls_obj_data-objla = sy-langu.
ls_obj_data-objlen = lines( it_content ) * 255.
data: wa_content type soli.
clear wa_content.
concatenate ‘&SO_FILENAME=’ ld_filename into wa_content.
append wa_content to it_objhead.
call function ‘SO_OBJECT_INSERT’
exporting
folder_id = ls_fol_id
object_type = ‘EXT’
object_hd_change = ls_obj_data
importing
object_id = ls_obj_id
tables
objhead = it_objhead
objcont = it_content
exceptions
active_user_not_exist = 35
folder_not_exist = 6
object_type_not_exist = 17
owner_not_exist = 22
parameter_error = 23
others = 1000.
if sy-subrc = 0 and ls_object-objkey is not initial.
commit work and wait.
ls_folmem_k-foltp = ls_fol_id-objtp.
ls_folmem_k-folyr = ls_fol_id-objyr.
ls_folmem_k-folno = ls_fol_id-objno.
ls_folmem_k-doctp = ls_obj_id-objtp.
ls_folmem_k-docyr = ls_obj_id-objyr.
ls_folmem_k-docno = ls_obj_id-objno.
lv_ep_note = ls_folmem_k.
ls_note-objtype = ‘MESSAGE’.
ls_note-objkey = lv_ep_note.
call function ‘BINARY_RELATION_CREATE_COMMIT’
exporting
obj_rolea = ls_object
obj_roleb = ls_note
relationtype = ‘ATTA’
exceptions
others = 1.
if sy-subrc eq 0.
es_return-text = ‘SUCCESS’.
commit work and wait.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error Calling: BINARY_RELATION_CREATE_COMMIT’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error Calling: SO_OBJECT_INSERT’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error With File Name’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error Obtaining Root Folder ID’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error Converting Table Contents to Binary’
receiving
rs_return = es_return.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘No Business Object and/or Key Specified’
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.
call function ‘F4_FILENAME’
exporting
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ‘P_FILE’
importing
file_name = 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.
else.
message t_st_return-text type ‘W’.
endif.
* ATTACH
move p_file to g_filename.
call function ‘GUI_UPLOAD’
exporting
filename = g_filename
filetype = ‘BIN’
importing
filelength = g_attsize
tables
data_tab = it_content
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
if sy-subrc eq 0.
data: l_file_name_with_path type avwctxcont.
move g_filename to l_file_name_with_path.
call method zcl_oh_my_gos=>gos_attach_file
exporting
objtype = p_type
objkey = p_key
folder_region = ‘B’
file_name_with_path = l_file_name_with_path
it_content = it_content
importing
es_return = t_st_return.
if t_st_return-code is initial.
message t_st_return-text type ‘I’.
else.
message t_st_return-text type ‘W’.
endif.
endif.
* 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.
write: /.
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.
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 = ltp_binfilesize
filetype = ‘BIN’
path_and_file = ltp_pathfile
extct = attachment-extct
no_dialog = ‘X’
importing
act_filename = ltp_filename
tables
objcont = lta_objcont
exceptions
file_write_error = 1
invalid_type = 2
x_error = 3
kpro_error = 4
others = 5.
if sy-subrc <> 0.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error Calling: SO_OBJECT_DOWNLOAD’
receiving
rs_return = es_return.
else.
es_return-text = ‘SUCCESS’.
endif.
else.
call method zcl_oh_my_gos=>log_msg
exporting
i_code = ‘021’
i_v1 = ‘Error With Object Import From DB’
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 = ‘021’
i_v1 = 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.
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.
call method zcl_oh_my_gos=>gos_download_file
exporting
file_path = ‘C:\TEMP’
attachment = lwa_sood
changing
es_return = t_st_return.
if t_st_return-code is initial.
dec_kb = lwa_sood-objlen / 1024.
if dec_kb < 1.
dec_kb = 1.
endif.
write: / lwa_sood-objdes, dec_kb, ‘KB’.
else.
message t_st_return-text type ‘W’.
endif.
endloop.
else.
message t_st_return-text type ‘W’.
endif.
I´d have add a method to "download" the file into an xString so it´d be possible to download it from a BSP, for example.
This is the code I made to do so:
METHOD gos_download_file_web.
DATA: attachment TYPE sood,
ex TYPE REF TO cx_root,
ltp_sortfield TYPE char30,
lta_objcont TYPE soli_tab,
temp_objcont TYPE soli,
temp_loio_id TYPE sdok_loid,
wa_soffphio TYPE soffphio,
objectid TYPE sdokobject,
documents TYPE TABLE OF soffphf,
document TYPE soffphf,
filecontent TYPE TABLE OF sdokcntbin,
file_access_info TYPE TABLE OF sdokfilaci,
wa_fai TYPE sdokfilaci,
temp_len TYPE i,
temp_fln TYPE i,
temp_lln TYPE i.
TRY.
READ TABLE t_attachments INTO attachment INDEX attch_num.
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.
READ TABLE lta_objcont INTO temp_objcont INDEX 1.
temp_loio_id = temp_objcont+27(32).
SELECT SINGLE * FROM soffphio INTO wa_soffphio
WHERE loio_id = temp_loio_id.
SELECT SINGLE * FROM soffphf INTO document
WHERE phio_id = wa_soffphio-phio_id.
IF sy-subrc EQ 0.
mimetype = document-mimetype.
IF mimetype IS INITIAL.
CASE attachment-file_ext.
WHEN 'DOC' OR 'doc'.
mimetype = 'application/msword'.
WHEN 'PDF' OR 'pdf'.
mimetype = 'application/pdf'.
ENDCASE.
ENDIF.
objectid-class = document-ph_class.
objectid-objid = document-phio_id.
CALL FUNCTION 'SDOK_PHIO_LOAD_CONTENT'
EXPORTING
object_id = objectid
TABLES
file_access_info = file_access_info
file_content_binary = filecontent
EXCEPTIONS
not_existing = 1
not_authorized = 2
no_content = 3
bad_storage_type = 4
OTHERS = 5.
READ TABLE file_access_info INTO wa_fai INDEX 1.
temp_len = wa_fai-file_size.
temp_fln = wa_fai-first_line.
temp_lln = wa_fai-last_line.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = temp_len
first_line = temp_fln
last_line = temp_lln
IMPORTING
buffer = docxstring
TABLES
binary_tab = filecontent.
IF sy-subrc = 0.
es_return = 0.
ENDIF.
ENDIF.
ENDIF.
CATCH cx_root INTO ex.
es_return = 3.
ENDTRY.
ENDMETHOD.
Has anyone any advice to improve it?? Anyway, it works...
This article has been VERY useful.
Thanks a lot.
I like it. Thanks for sharing..
Hi experts,
Maybe you can help me with a big challenge here. I need to migrate some materials attachment (MM03), these materials have been migrated from SAP to SAP systems and now I need to put them attachments massively. The two SAP systems are
connected through RFC.
I used some FM as BDS_GOS_CONNECTIONS_GET and SO_DOCUMENT_READ_API1 to get the attachment in binary mode. So at this time I
don’t know how to insert these
attachments in another SAP system.
So, how to insert these attachments in the corresponding material
through RFC? I did not find a GOS FM to insert it.
Thank you in advanced
Antonio Rocha
Antonio, you can use the Methods above to solve you challenge. There are two ways you can approach:
1. Use the GOS_GET_FILE_LIST method and loop through results. In your loop,
call method GOS_DOWNLOAD_FILE to save all files to a folder on your hard
drive.
Then in your receiving system, call WS_UPLOAD and loop through all the files
calling method GOS_ATTACH_FILE to put the files into the receiving system.
2. Since you mentioned the systems are connected, I would use a similar
approach but use the RFC destination option on your function calls so you can
bypass the download to hard drive step.
Should be fairly easy to do.
thanks, Phil
Thanks for your help, Philip. I will get an abaper and We'll try to do that through RFC approach.
Antonio
Hello Philip,
Do you know if the method to attach files into GOS transactions works in backgroud mode also?
Thanks!
Yes. it can be used for background batch uploads.
Hello Philip,
I have a PO number at my report output and I want to add attachments (attachment present at presentation server ) from my report to the PO level. Please suggest me only adding the attachment from the report to the PO level.
Thanks,
RG
Hi, can you show the definition on SE11 of your table :ZVNT_ST_RETURN and Z_TT_SOLI i would be very helpfull i know its a couple of years later but i would like you see it if it's possible.
Thank you in advance.
Andrei Cadar
Hi Philip,
Thanks for your document. It really helps.
Could you please let me know and provide help if you have worked on creating URLs?
regards
kanishak
Thank you for the class, it was exactly what I needed. Have you considered opening a repo to host it on github?
Thank you for an excellent blog mate, it is really helpful.
Could you please also explain the structure of custom objects ZVNT_ST_RETURN and Z_TT_SOLI.
Regards,
Dharmesh
I have used the below structure and was able to successfully execute this above code.
ZVNT_ST_RETURN
Z_TT_SOLI -> use this table type instead SOLI_TAB
Method --> LOG_MSG
The entire code, in a report, for testing . Just copy and paste in a report. Only that.