Friday, July 20, 2007

Get Document / File stored in SAP

Previous post: Store Document / File to SAP

Get document / file stored in SAP Business Document.


REPORT ZAALTES1 line-size 300.

INCLUDE officeintegrationinclude. " used by DOI
* Get descr_list
TYPES: BEGIN OF SOI_DOCUMENT_TYPE_DESCR,
DOCUMENT_TYPE TYPE SOI_DOCUMENT_TYPE, " prog id
TYPE_FULL_NAME(70) TYPE C, " full name
TYPE_SHORT_NAME(20) TYPE C, " short name
APPLICATION_NAME(50) TYPE C, " application name
SUPPORTED_INTERFACES(256) TYPE C, " supported SAP interfaces
AVAILABLE(1) TYPE C, " X on return when available
END OF SOI_DOCUMENT_TYPE_DESCR.
DATA:
descr_list TYPE soi_document_type_descr_list,
wa_descr_list TYPE SOI_DOCUMENT_TYPE_DESCR,
retcode TYPE t_oi_ret_string.
DATA: application_cntl TYPE REF TO i_oi_ole_container_control.
* ----------

* -- connection table and the internal structure of the ITAB
DATA: BEGIN OF i_bds_conn OCCURS 10. " ITAB for the actual
INCLUDE STRUCTURE bdn_con. " document
DATA: objecttext LIKE toasp-objecttext,
objecttext2 LIKE toasd-objecttext,
objecttext3 LIKE toasr-objecttext,
ntext LIKE tojtt-ntext,
END OF i_bds_conn.
DATA: BEGIN OF bds_doctype_list OCCURS 10,
mandt TYPE mandt,
classname LIKE bapibds01-classname,
contrep LIKE i_bds_conn-contrep,
docuclass LIKE i_bds_conn-docuclass,
docuclass_text LIKE toasd-objecttext,
doc_type LIKE i_bds_conn-doc_type,
doc_type_text LIKE toasp-objecttext,
appl_type LIKE toadd-appl_type,
appl_type_text(50) TYPE c,
standard LIKE toadv-standard,
check_box LIKE toadv-standard,
END OF bds_doctype_list.

TYPES: ole2_application(70) TYPE c.

DATA:
logical_system LIKE bds_conn00-log_system,
display_struc LIKE i_bds_conn OCCURS 2 WITH HEADER LINE,
classname_select LIKE bdn_con-classname,
classtype_select LIKE bdn_con-classtype,
objkey_select LIKE bdn_con-objkey,
mask(20) TYPE c,
signature LIKE bapisignat OCCURS 1 WITH HEADER LINE,
build_all(1) TYPE c, " get all OLE apps
ole2_app TYPE ole2_application,
i_ole2_app_text LIKE bds_doctype_list-appl_type_text,
d_tabix LIKE sy-tabix,
vartemp
.
TYPES: BEGIN OF ole_apps_struc,
appl_type LIKE toadd-appl_type,
appl_type_text LIKE bds_doctype_list-appl_type_text,
mimetype LIKE bdn_con-mimetype,
docuclass LIKE bdn_con-docuclass,
END OF ole_apps_struc.
DATA: ole_apps TYPE ole_apps_struc OCCURS 5.

PARAMETERS: p_class LIKE bdn_con-classname OBLIGATORY,
p_objct LIKE bdn_con-objkey OBLIGATORY.


start-of-selection.
classname_select = p_class.
classtype_select = 'OT'.
objkey_select = p_objct.
PERFORM f_bds_call_navigator.

at line-selection.
PERFORM f_line_selection.


FORM f_bds_call_navigator.
* Function : BDS_CALL_NAVIGATOR
* -- data declaration -- *
DATA: i_connections LIKE bdn_con OCCURS 0 WITH HEADER LINE,
calling_signa LIKE bapisignat OCCURS 0 WITH HEADER LINE,
i_count LIKE sy-index,
i_gui_type LIKE bapibds01-type,
web_excluding LIKE bdn_fkt OCCURS 1 WITH HEADER LINE.

* -- get all connections for the objtype/objkey -- *
CALL FUNCTION 'BDS_ALL_CONNECTIONS_GET'
EXPORTING
logical_system = logical_system
classname = classname_select
classtype = classtype_select
objkey = objkey_select
client = sy-mandt
all = ' ' " <- get the newest version
IMPORTING
count = i_count
TABLES
signature = signature
all_connections = i_connections
* framework = framework
* -- Begin correction 388230 ------------------------------ *
* EXCEPTIONS
* no_objects_found = 1
* error_kpro = 2
* internal_error = 3
* OTHERS = 4.
* IF sy-subrc > 1.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
* RAISING internal_error.
* ENDIF.
EXCEPTIONS
no_objects_found = 1
error_kpro = 2
internal_error = 3
not_authorized = 4
OTHERS = 5.
*End Function : BDS_CALL_NAVIGATOR

*Function : BDS_DISPLAY_LIST_VIA_TREE
* -- get some text fields -- *
DATA:
i_toasp LIKE toasp OCCURS 1 WITH HEADER LINE,
i_toasd LIKE toasd OCCURS 1 WITH HEADER LINE,
i_toasr LIKE toasr OCCURS 1 WITH HEADER LINE,
class_text like tojtt-ntext
.
SELECT * FROM toasp INTO TABLE i_toasp WHERE language = sy-langu.
SELECT * FROM toasd INTO TABLE i_toasd WHERE language = sy-langu.
SELECT * FROM toasr INTO TABLE i_toasr WHERE language = sy-langu.
APPEND LINES OF i_connections TO i_bds_conn.
LOOP AT i_bds_conn.
IF i_bds_conn-doc_type <> space. " -> Corr. (402788) A.M.
READ TABLE i_toasp WITH KEY ar_object = i_bds_conn-doc_type.
IF sy-subrc = 0.
i_bds_conn-objecttext = i_toasp-objecttext.
ENDIF.
ENDIF. " -> Corr. (402788) A.M.
READ TABLE i_toasr WITH KEY archiv_id = i_bds_conn-contrep.
IF sy-subrc = 0.
i_bds_conn-objecttext3 = i_toasr-objecttext.
ENDIF.
IF i_bds_conn-docuclass <> space.
READ TABLE i_toasd WITH KEY doc_type = i_bds_conn-docuclass.
IF sy-subrc = 0.
i_bds_conn-objecttext2 = i_toasd-objecttext.
ENDIF.
ENDIF.

i_bds_conn-ntext = class_text.

MODIFY i_bds_conn.
ENDLOOP.

*End Function : BDS_DISPLAY_LIST_VIA_TREE
skip.
LOOP AT i_bds_conn.
d_tabix = sy-tabix.
HIDE d_tabix.
WRITE : / i_bds_conn-BDN_TAB_I,
i_bds_conn-descript,
(50) i_bds_conn-comp_id,
i_bds_conn-objecttext,
i_bds_conn-objecttext2,
i_bds_conn-objecttext3,
i_bds_conn-ntext.

ENDLOOP.
CLEAR d_tabix.

EXIT.

PERFORM f_get_registered_doc_types.
*Program: LBDS_TOOLSF01, Form: display_selected_document
MOVE-CORRESPONDING i_bds_conn TO display_struc.
* -- find ole2_app -- *
build_all = ' '.
PERFORM get_ole2_app2 TABLES descr_list
USING display_struc-docuclass
display_struc-mimetype
ole2_app i_ole2_app_text
build_all.


ENDFORM. "f_bds_call_navigator
*&---------------------------------------------------------------------*
*& Form display_selected_document_out
*&---------------------------------------------------------------------*
* display selected document NOT in-place.
*----------------------------------------------------------------------*
FORM display_selected_document_out
TABLES display_struc STRUCTURE i_bds_conn.

* -- data declaration -- *
DATA: display_signature LIKE bapisignat OCCURS 5 WITH HEADER LINE.
* -------------------------------------------------------------------- *

CLEAR: display_signature.
REFRESH: display_signature.

READ TABLE display_struc INDEX 1.

display_signature-doc_count = '1'.
display_signature-doc_id = display_struc-loio_id.
display_signature-doc_ver_no = display_struc-doc_ver_no.
display_signature-doc_var_id = display_struc-doc_var_id.
display_signature-doc_var_tg = display_struc-doc_var_tg.
APPEND display_signature.

CALL FUNCTION 'BDS_OBJECT_DISPLAY_INTERN'
EXPORTING
logical_system = display_struc-log_system
classname = display_struc-classname
classtype = display_struc-classtype
client = display_struc-client
object_key = display_struc-objkey
bds_docid = display_struc-bds_docid
bds_contrep = display_struc-contrep
bds_docuclass = display_struc-docuclass
TABLES
signature = display_signature
EXCEPTIONS
error_kpro = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.


ENDFORM. " display_selected_document_out

*&---------------------------------------------------------------------*
*& Form GET_OLE2_APP2
*&---------------------------------------------------------------------*
* Get the correct OLE-App.
*----------------------------------------------------------------------*
FORM get_ole2_app2 TABLES descr_list
USING i_docuclass LIKE bdn_con-docuclass
i_mimetype LIKE bdn_con-mimetype
ole2_app LIKE toadd-appl_type
ole2_app_text LIKE bds_doctype_list-appl_type_text
build_all LIKE bdn_con-stor_tab.
* -- Data declaration ------------------------------------------------ *
TYPES: BEGIN OF document_type_descr,
document_type(70) TYPE c,
type_full_name(70) TYPE c,
type_short_name(20) TYPE c,
application_name(50) TYPE c,
supported_interfaces(256) TYPE c,
available(1) TYPE c,
END OF document_type_descr.

DATA: i_ole2_app TYPE toadd OCCURS 1 WITH HEADER LINE,
descr_list_struc TYPE document_type_descr,
local_langu LIKE sy-langu,
ole_apps_line TYPE ole_apps_struc,
temp_mimetype LIKE bdn_con-mimetype.

* -- Initilize Data -------------------------------------------------- *
local_langu = sy-langu.
* -------------------------------------------------------------------- *

IF build_all = ' '. " -> get a special ole_type
SET LOCALE LANGUAGE local_langu.
TRANSLATE i_mimetype TO LOWER CASE.
SET LOCALE LANGUAGE space.

IF i_docuclass <> space.
SELECT * INTO TABLE i_ole2_app FROM toadd
WHERE doc_type = i_docuclass.
temp_mimetype = i_mimetype.
i_mimetype = space.
ELSEIF i_mimetype <> space.
SELECT * INTO TABLE i_ole2_app FROM toadd
WHERE mimetype = i_mimetype.
i_docuclass = space.
temp_mimetype = i_mimetype.
ENDIF.
CLEAR i_ole2_app.
LOOP AT i_ole2_app.
IF i_ole2_app-appl_type <> space.
IF ( i_ole2_app-appl_type = 'EAIWEB.WEBVIEWER2D' OR
i_ole2_app-appl_type = 'EAIWEB.WEBVIEWER3D' OR
i_ole2_app-appl_type = 'SAPHTML' ).
ole2_app = i_ole2_app-appl_type.
i_mimetype = temp_mimetype.
EXIT.
ENDIF.
LOOP AT descr_list INTO descr_list_struc.
SET LOCALE LANGUAGE local_langu.
TRANSLATE descr_list_struc-document_type TO UPPER CASE.
SET LOCALE LANGUAGE space.
IF i_ole2_app-appl_type = descr_list_struc-document_type.
ole2_app = i_ole2_app-appl_type.
ole2_app_text = descr_list_struc-application_name.
i_docuclass = i_ole2_app-doc_type.
i_mimetype = i_ole2_app-mimetype.
EXIT.
ENDIF.
ENDLOOP.
ELSE.
ole2_app = space.
ole2_app_text = space.
i_mimetype = i_ole2_app-mimetype.
i_docuclass = i_ole2_app-doc_type.
EXIT.
ENDIF.
ENDLOOP.
ELSE. " -> get the complete list
CLEAR i_ole2_app.
SELECT * INTO TABLE i_ole2_app FROM toadd.
LOOP AT i_ole2_app.
IF ( i_ole2_app-appl_type <> space AND
i_ole2_app-appl_type <> 'ACROEXCH.DOCUMENT' ). "#EC NOTEXT
LOOP AT descr_list INTO descr_list_struc.
SET LOCALE LANGUAGE local_langu.
TRANSLATE descr_list_struc-document_type TO UPPER CASE.
SET LOCALE LANGUAGE space.
IF i_ole2_app-appl_type = descr_list_struc-document_type.
ole_apps_line-appl_type = i_ole2_app-appl_type.
ole_apps_line-appl_type_text =
descr_list_struc-application_name.
ole_apps_line-docuclass = i_ole2_app-doc_type.
ole_apps_line-mimetype = i_ole2_app-mimetype.
APPEND ole_apps_line TO ole_apps.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.

SORT ole_apps BY appl_type.
DELETE ADJACENT DUPLICATES FROM ole_apps COMPARING appl_type.

ENDIF.

ENDFORM. " GET_OLE2_APP2
*&---------------------------------------------------------------------*
*& Form f_get_registered_doc_types
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_get_registered_doc_types.
if application_cntl is initial.
* -- Create Container Control -- *
call method c_oi_ole_control_creator=>get_ole_container_control
importing
control = application_cntl
retcode = retcode.

if retcode <> c_oi_errors=>ret_ok.
call method c_oi_errors=>show_message
exporting
type = 'E'.
endif.

call method application_cntl->get_registered_doc_types
exporting
interface_type = ' '
importing
descr_list = descr_list
retcode = retcode.

if retcode <> c_oi_errors=>ret_ok.
call method c_oi_errors=>show_message
exporting
type = 'E'.
endif.

endif.
ENDFORM. " f_get_registered_doc_types
*&---------------------------------------------------------------------*
*& Form f_line_selection
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_line_selection.
READ TABLE i_bds_conn INDEX d_tabix.
CHECK sy-subrc = 0.
MOVE-CORRESPONDING i_bds_conn TO display_struc.

PERFORM display_selected_document_out TABLES display_struc.


CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
IF sy-subrc <> 0.
MESSAGE e324(sbds).
ELSE.
WRITE :/ 'success'.
ENDIF.

ENDFORM. " f_line_selection

9 comments:

SAPna said...

Another nice piece of code.

I was trying to list all the documents for ALL the ZCLASSES. (Currently, you pgm only lists files for a single class). I changed the parameter to Select-options but I was unable to loop because I don't know the table behind the FM.

Thanks

alionzo said...

Thank for your kindly comment.
Use table BDS_LOCL

SAPna said...

Alionzo,

Thanks for the table name. It is interesting to note that there are 231 such tables (BDS*).

Is that true that OAOR is the tcode that enables user to view files.

Thanks

alionzo said...

Yes, it's true, tcode: OAOR.

Unknown said...

Please give me some documents related to abap assignment and sylabus

zee said...

Hi. This is excellent. I used your Store Document program to store a Word Doc and then used this program to retrieve it. But what do I need to do if I want to display/open the Word document that I stored?

Anonymous said...

Hi Alionzo,

Do you have sample code to save the document locally (own PC) instead of displaying the document in a new window?

Looking forward to your reply.

Thanks,
Joan

Anonymous said...

If you store a file, will that be available in SAPGUI via the 'paperclip'? In other words, do I need this code for displaying if I'm storing via ABAP program, but retrieving through SAPGUI?

QuickBooks Support Phone Number said...

It offers tons of features along with great UI. Besides, if you get struck anywhere then dial QuickBooks
Customer Care Number +1-844-232-O2O2 and get it fixed easily. Call right away!
https://www.edocr.com/v/5nwmynek/rsm93658/Learn-more-about-the-remarkable-features-of-QuickB
https://issuu.com/rogersmith31/docs/learn_more_about_the_remarkable_features_of_quickb
https://www.edocr.com/v/pmkw5z1r/rsm93658/QuickBooks-Enterprise-Support-Phone-Number-1-844-2
https://issuu.com/rogersmith31/docs/quickbooks_enterprise_support_phone_number__1-844-