Thursday 8 December 2011

Store Document / File to SAP

To store document / image in SAP, we can use Business Document Navigator.
To go to the Business Document Navigator, choose Office -> Business Documents -> Documents -> Find (transaction code : OAOR).

In the upper part of the screen, the relevant documents, sorted by document type (the document types in turn belong to specified application objects) are displayed in the tree. The lower left part of the screen contains tab pages with the functions Detailed display, Document information (version string), Keywords and Storing. On the right-hand side of the screen, you can display a selected document in-place.

A bundled document structured by Class Name, Class Type and object Key. We can add additional attributes for document, including key word, title, description.

To save and get file programmatically, here are the codes.

I split them into two program, store document and get document. Program to get and display document can be found in next post.
Program to store document:

REPORT ZAALGAL0002 NO STANDARD PAGE HEADING.
* Store document in BDN

* -- 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.
DATA: file_extension LIKE toadd-doc_type, " file-extension
i_files LIKE bapifiles OCCURS 1 WITH HEADER LINE,
i_signature LIKE bapisignat OCCURS 1 WITH HEADER LINE,
logical_system LIKE bds_conn00-log_system.

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

start-of-selection.

PERFORM create_doc_via_file.

*&---------------------------------------------------------------------*
*& Form CREATE_DOC_VIA_FILE
*&---------------------------------------------------------------------*
* create a new document via file
*----------------------------------------------------------------------*
FORM create_doc_via_file.
* -- data declaration ------------------------------------------------ *
DATA: classname_select LIKE bdn_con-classname,
classtype_select LIKE bdn_con-classtype,
objkey_select LIKE bdn_con-objkey,
mask(20) TYPE c,
answer TYPE c,
mimetype LIKE toadd-mimetype,
i_toadd LIKE toadd,
filename_all LIKE sapb-sapfiles,
file_path LIKE sapb-sapfiles,
file_path_memory(250) TYPE c, " path für SAP memory
file_path_length TYPE i, " length of the file_path
file_name LIKE sapb-sapfiles.


classname_select = p_class.
classtype_select = 'OT'.
objkey_select = p_objct.


GET PARAMETER ID 'OAP' FIELD file_path.
IF sy-subrc <> 0. " no file_path found.
file_path = space.
ENDIF.

CONCATENATE ',*.' '*' ',*.' '*' '.' INTO mask.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = file_path
mask = mask
mode = 'O'
title = 'Select File to upload'
IMPORTING
filename = filename_all
EXCEPTIONS
inv_winsys = 01
no_batch = 02
selection_cancel = 03
selection_error = 04.

IF sy-subrc = 3.
MESSAGE s012(sbds). " cancel file selection
EXIT.
ELSEIF sy-subrc <> 0 AND sy-subrc <> 3.
MESSAGE e011(sbds). " error during file selection
ENDIF.

* -- split filename -- *
PERFORM split_path(oaall) USING filename_all file_path file_name.

* -- set new file_path to SAP memory -- *
file_path_length = strlen( file_path ).

IF file_path <> space AND file_path_length < 250.
file_path_memory = file_path.
SET PARAMETER ID 'OAP' FIELD file_path_memory.
ELSE.
file_path_memory = space.
SET PARAMETER ID 'OAP' FIELD file_path_memory.
ENDIF.

* -- check documentclass -- *
PERFORM get_file_extension USING file_name
file_extension.

* -- check file extension -- *
IF file_extension = space.
* -> if no docuclass is found from the document
* -> default docuclass from the doctype!
file_extension = bds_doctype_list-docuclass.
ENDIF.

* -- get the mimetype of the docuclass -- *
PERFORM mimetype_get(oaall) USING file_extension
CHANGING i_toadd.
MOVE i_toadd-mimetype TO mimetype.

* -- fill file and signature structure -- *
CLEAR: i_files, i_signature.
REFRESH : i_files, i_signature.

i_files-doc_count = 1.
i_files-directory = file_path.
i_files-filename = file_name.
i_files-mimetype = mimetype.
APPEND i_files.

i_signature-doc_count = 1.
i_signature-prop_name = 'BDS_DOCUMENTCLASS'.
i_signature-prop_value = file_extension.
APPEND i_signature.
i_signature-prop_name = 'BDS_CONTREP'.
IF bds_doctype_list-contrep = space.
i_signature-prop_value = ' '. "#EC NOTEXT
ELSE.
i_signature-prop_value = bds_doctype_list-contrep.
ENDIF.
APPEND i_signature.
i_signature-prop_name = 'BDS_DOCUMENTTYPE'.
i_signature-prop_value = bds_doctype_list-doc_type.
APPEND i_signature.
i_signature-prop_name = 'DESCRIPTION'.
i_signature-prop_value = p_descr.
APPEND i_signature.
i_signature-prop_name = 'LANGUAGE'.
i_signature-prop_value = sy-langu.
APPEND i_signature.

* -- create new document via KPro -- *
CALL FUNCTION 'BDS_BUSINESSDOCUMENT_CREATEF'
EXPORTING
logical_system = logical_system
classname = classname_select
classtype = classtype_select
client = sy-mandt
object_key = objkey_select
TABLES
files = i_files
signature = i_signature
EXCEPTIONS
internal_error = 1
OTHERS = 2.

WRITE:/ 'sy-subrc:',sy-subrc.



ENDFORM. " CREATE_DOC_VIA_FILE
*&---------------------------------------------------------------------*
*& Form GET_FILE_EXTENSION
*&---------------------------------------------------------------------*
* try to get the extension of the uploaded file
*----------------------------------------------------------------------*
FORM get_file_extension USING file_name file_extension.
* -- data declaration ------------------------------------------------ *
DATA: length TYPE i,
single_c TYPE c.
* -------------------------------------------------------------------- *
CLEAR: single_c.

length = strlen( file_name ).
IF length > 0.
WHILE length > 0.
single_c = file_name+length(1).
IF single_c CO '.'.
length = length + 1.
EXIT.
ELSE.
length = length - 1.
ENDIF.
ENDWHILE.
IF length > 0.
file_extension = file_name+length.
ELSE.
file_extension = space.
ENDIF.
ELSE.
file_extension = space.
ENDIF.

IF file_extension <> space.
SET LOCALE LANGUAGE sy-langu.
TRANSLATE file_extension TO UPPER CASE. "#EC TRANSLANG
SET LOCALE LANGUAGE space.
ENDIF.

ENDFORM. " GET_FILE_EXTENSION

No comments:

Post a Comment