Thursday, July 19, 2007

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:

* 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.


PERFORM 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.

IF sy-subrc <> 0. " no file_path found.
file_path = space.

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

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

* -- 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.
file_path_memory = space.
SET PARAMETER ID 'OAP' FIELD file_path_memory.

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

* -- 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.

* -- 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
i_signature-prop_value = bds_doctype_list-contrep.
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 -- *
logical_system = logical_system
classname = classname_select
classtype = classtype_select
client = sy-mandt
object_key = objkey_select
files = i_files
signature = i_signature
internal_error = 1

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

* 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.
length = length - 1.
IF length > 0.
file_extension = file_name+length.
file_extension = space.
file_extension = space.

IF file_extension <> space.



marge said...

Hi sir!
I am trying to run your program...How exactly do you know what to enter for the class name and object key? Do I need to create an application object first?

abap-gallery said...

Hi Marge,
When you are using transaction OAOR to store document manually, first you have to maintain table class in tcode SBDSV1. But if you use this program, you can enter antything for class name and object key without maintaining class.
To avoid confusion with SAP object I sugget you to use Z as prefix for class name and use document no as object key to make it easier to read / find.
For example, if you want to use it to store supporting document for sales order (table VBAK), use this :
classname: ZVBAK
object key:000XX (order number)

naved said...

Nice piece of code.

Is there a limitation on Number of zClasses I can create. I am planning to create one zclass and one zobjkey (1-1 relationship) for each record/document. These are not SAP std documents. They are created for every record. There will be about 1,000 records. Can I create 1,000 zclasses?


alionzo said...

Hi Naved,
No limitation, you can create 1000 classes. But, I suggest to create one class for one transaction to make it easier to maintain and better performance.

Anonymous said...

Good Day!
THis is out of your topic but maybe you could help me. Im developing a tool from excel vba with connection to sap. I already know how to connect (GUI).
But my problem is that I don't know the object names to point with. ex. Session.FindByID("wnd[0]/usr/?????") if Im gonna make use of the command box for instance.

alionzo said...

wow.. I never heard this technique before, can you share to me where to find the information on using how to use Session.FindID ?
We usually using BAPI / Remote Function Module to connect to SAP from VBA by using wdtfuncs.ocx .

sandeep said...

hi Sir!

i want to show the photo on the module pool screen which is to be fetched from the PC and will be stored in database with every record. please suggest me how can i do so.....

Sandeep Chauhan

alionzo said...

Hi Sandeep,

To figure out how SAP open program in SAP subscreen, open program LBDS_TOOLSF01, create breakpoint in routine "display_selected_document".

Then, goto TCode OAOR. Open image already uploaded, it will halt at break point created before.

SAP using object CL_GUI_HTML_VIEWER to open image in SAP screen.

Sorry I can give you the detail, but may be you can use google to search on term "CL_GUI_HTML_VIEWER"

Hope it help

Anonymous said...

i want to capture the screen shot of a SAP output into presentation server (any given path).How will that be possible.

Kind Regards,

Anonymous said...

Hi alionzo,

ur code is really helpful...
But By default document store in C:\Documents and Settings\abap1sap\SapWorkDir\
Can we change the path of storing document?

SAP Handbook said...

This is indeed useful. Pls. help me, i am not able to transport the documents uploaded in OAOR transaction code.
I have uploaded as
class name: ZALERT
Class type: OT
Object key: ZALERT

Thanks and Regards,
Ravi Kajaria.

Leviana said...

This application is to hard for me to use, I prefer a simple at that allow me to find blog related to Buy Generic Viagra fast.

VISHWAS said...

Hi ,
for saving file against employee.
i m following the steps as mention such as TC:SBDSV1,here i mention=>Class name:ZEMPCV,Class type:OT,DocuClass:BDS_LOC1,BDS_POC1,BDS_REC1,Object Name:ZEMPCV(i just mention Object name without ccreating it).after that i run program as mention 'ZAALGAL0002.i mention CLASS:ZEMPCV
OBJCT:ZEMPCV and descr:xyz.But i wil get output with sy-subrc = 1 . so pls guide me how to save file against employee.
thanks in advance

Anonymous said...

is it possible to store a document into the business document navigator from outside of sap, such as a email or a group of scanned files?

seventyros said...

For a Excellent Online Resource for SAP EP and SAP Web Dynpro ABAP, Visit Learn SAP Online

Anonymous said...

This is Gold Men, Thanks. I like to know how configure an external repository for the documents, if anyone have information about this matter please post-it

Thanks in advance

Juan Camilo

$HIV@ said...

Hi sir,

I have uploaded 2 images under same object-id in OAOR.. how to display the second image in the screen... i used the add_picture method of cl_dd_document .. but how to use index that,..

mani said...

Iam really satisfy by your information.
It's well-written, to the point, and relative to what I do.
I like it very much for giving information on
SAP ABAP Online Training


yektek training said...

Thanks for this post sravani

kits online said...

very nice article.Thanks for sharing the post..!
Ruby On Rails Online Training

Shri Veera said...

Thank you for sharing very informatics and useful post about very useful info.... Turbo-IVP (Invoice Validation Portal) and Turbo eSigner