Create an SAP DMS document attaching a file from the Application Server and checking in an FTP vault with automatic filename creation
Hi all,
So, as you can see from the title, I had a very specific requirement. I needed to be able to create a document (those you can open through CV03N) programmatically attaching a file from the application server and with an FTP server that automatically assigns filenames.
I searched google/scn extensively for a way to do this or an example and tried a few, but it seems that the configuration options for DMS are so vast that it was unlikely to find an example that suited my needs and my configuration. So I’m sharing my code and my example, hoping that maybe it will be helpful for someone else, and maybe I’ll even need it myself in the future.
The code ended up being extremely simple, but I assure you I tried so many really complex approaches that I don’t even want to remind myself of them again
DATA: lv_docty TYPE dokar, lv_docnr TYPE doknr, lv_docpr TYPE doktl_d, lv_docvr TYPE dokvr, ls_ret TYPE bapiret2, lt_files TYPE TABLE OF bapi_doc_files2, ls_files LIKE LINE OF lt_files, ls_documentdata TYPE bapi_doc_draw2. CLEAR ls_documentdata. ls_documentdata-documenttype = 'ZCL'. ls_documentdata-description = 'Testing DMS'. ls_documentdata-username = sy-uname. CLEAR ls_files. ls_files-originaltype = '1'. ls_files-storagecategory = '<VAULT NAME HERE>'. ls_files-docpath = '/tmp/'. ls_files-docfile = 'dms_test4.pdf'. ls_files-wsapplication = 'PDF'. APPEND ls_files TO lt_files. CALL FUNCTION 'BAPI_DOCUMENT_CREATE2' EXPORTING documentdata = ls_documentdata pf_ftp_dest = 'SAPFTPA' pf_http_dest = 'SAPHTTPA' IMPORTING documenttype = lv_docty documentnumber = lv_docnr documentpart = lv_docpr documentversion = lv_docvr return = ls_ret TABLES documentfiles = lt_files. IF ls_ret-type NE 'E'. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' . ENDIF.
And the result:
I hope this will be helpful to someone!
Best regards,
Bruno
Thanks Bruno.
THis is very good code which can be used for various scenarios.
With Warm Regards
Mangesh Pande
Thanks, appreciate it!
Bruno
what is the value of document type if we attach file for vendor(XK02/XK03)?
I am just struggling with this type of problem and was very happy to find your example. Unfortunately I get an upload error 😥
In my case I am generating a file on the application server and it is to be stored in the SAP system/database (not using Knowledge Provider).
When manually creating a document and attaching such a file it gets automatically assigned to SAP-SYSTEM as archive category. I have tried both with and without specification of this value to field “STORAGECATEGORY”, but have not solved the problem yet.
Do you have any experience with that or a tip where I could start investigating?
Hi Kjetil!
I'm glad my example made you happy somehow 🙂
Like I said, before being successful I tried many more complex ways of achieving what I wanted. The best thing that I did was going home, sleeping, and coming back to office fresh the day after 🙂 My approach for the next day was: let's simplify, and if it's automatic, let's leave it OUT of the equation.
From what you're telling me it seems you don't have an FTP server configured, so, not only you should leave STORAGECATEGORY blank but also the pf_ftp_dest line should probably be commented out too, since there will be no FTP server (or at least that's how it seems to me). You should also look around in configuration (SPRO - Cross-Application Components - Document Management - Control/General Data) to see how your DMS is configured, if you weren't the one configuring it! 🙂
It's funny... your requirement seems to be easier than mine... but with SAP sometimes easier is more difficult! 😀
Good luck... keep me posted of your results. I could actually extend my document to include your case and your solutions. This way it will be easy to find for us if we ever need it in the future! 🙂
Regards,
Bruno
In another thread ('BAPI_DOCUMENT_CREATE2': store attachment using 'SAP-SYSTEM') I found an important piece of information - the files which are not to be stored using Knowledge Provider are supplied as part of the document data (BAPI_DOC_DRAW2) structure.
So, by filling the fields DOCFILE1, DATACARRIER1 (“SAP-SYSTEM”) and WSAPPLICATION1 the original file got added and checked in - at least so it appears.
Now I get an error (26/254) when trying to display the original, but at least I got one step further - I think and hope.
Hi Kjetil,
Glad you're making some progress 🙂
Error while opening the file... At first glance I would say maybe the file you are trying to open is not maintained in the workstation application customizing table. Check transaction code DC30 or search for "Define Workstation Application" in SPRO. That would be my best guess...
I just looked at the error message... Error while checking out... so I don't think it's what I just said. Maybe debugging will help you? 🙁
I don't think I have this set up so that I can store documents in SAP-SYSTEM, otherwise I would try to have a look as well.
Good luck!
Bruno
After trying to solve the checkout problem for hours and hours without getting any wiser I debugged far enough down in the SAP code to discover that the check-in is the problem. There is no content in table DRAO for the originals I have attached after creating them on the server. 😥
Hi Kjetil,
Well... at least that's some progress!! 🙂
I see table DRAO being used in Web Dynpro component LFS_SUST_FILEUPLOAD. If you look at method PROCESS_EVENT in its component controller you see very interesting code that might be helpful for you. Namely:
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING: buffer = lf_content
IMPORTING: output_length = lv_len
TABLES: binary_tab = lt_binary.
LOOP AT lt_binary ASSIGNING <fs>.
ls_drao-orblk = <fs>-line.
ls_drao-orln = lv_len.
APPEND ls_drao TO lt_content.
ADD 1 TO ls_drao-zaehl.
ENDLOOP.
See if that helps!!
Regards,
Bruno
Hi Kjetil!
Any luck? I was hoping that my finding above could maybe help you "crack" the solution to your requirement.
Cheers!
Bruno
Sorry about the delay. I have confirmed the concept, but due to other tasks I have not completed the solution yet. I *will* get back to this.
Hi Kjetil,
Interestingly enough I think I'm going to need this to. I am working on it now 😛
Cheers,
Bruno
So I'm glad I shared that piece of information about component controller LFS_SUST_FILEUPLOAD 😀
Sorry about taking so long before updating this thread, but here is the solution that turned out to work in my case.
First of all, the case can be described as follows:
- Document type is not using Knowledge Provider
- Original file is a small text file created on the fly in a web service implementation
- Original file stored in the TMP area during web service execution
The solution in our case ended along the following pseudo-code lines:
1) Create original file on server TMP area (with fixed name)
2) Call BAPI_DOCUMENT_CREATE2 - not passing on original created in step 1
3) Call BAPI_TRANSACTION_COMMIT (if creation was successful, otherwise exit)
4) Read file from server
5) Convert text file contents to binary content format using SCMS_STRING_TO_XSTRING followed by SCMS_XSTRING_TO_BINARY
6) Attach original to document using CVAPI_DOC_CHECKIN
The first and fourth steps could probably be skipped, i.e. the file contents could be passed on directly to SCMS_STRING_TO_XSTRING, but I haven't touched it much after I finally got it working 🙂
Hi kjetil/bruno,
i am also stuck up with same kind of requirment.here,i am having the document content downloaded from xECm to my internal table in xstring format.now,i want to store it in a word file in application server without saving it in presentation layer .can you please guide me.
Regards,
Krishna.
Hi Krishna,
I think the easiest way for you is to create this document in a temporary location in the application server using the OPEN DATASET statement, and then follow my instructions above.
I believe that the OPEN DATASET need the data in binary format, but you should be able to find a way to convert easily by searching the forum.
If you encounter any specific issues please share the code and maybe we help.
Good luck.
Kind regards,
Bruno
Hi bruno,
1. created a file in application server using the opendata set stmt.
open dataset dsn for output in binary mode .
loop at lt_binary into wa_binary .
transfer wa_binary to file .
endloop.
while sy-subrc = 0 .
transfer file to result .
endwhile.
close dataset file .
now ,when i open the file in the al11 ...it is opening in unreadable format.
i want to display the file in word document .without downloading it in to presentation layer.
Hi Krishna,
I'm not really sure what you are trying to achieve. To open and display a file you always have to create a local copy. For example, when you are browsing the internet and you choose to "open" a file instead of saving it, a local copy is created in your temporary files, and then eventually it gets deleted when you clear the temporary files from the browser cache.
Best regards,
Bruno
hi bruno ,
the requirment is to control the printing of the document ,previously while printing they are updating the document in the dms using ole.but,now the dms is replaced by xECM .from xecm we are getting the document attachment as xstring .
we can edit the document by downloading it into presentation server .but the requirment is to control the printing of the document ,hence we have to achive printing of the document by user without downloading it into the presntation layer.
please share your mail id ,so that i can share code also.
Regards,
Krishna.
Hi Krishna,
I don't think I can help you much further with this. Try and open a new question and share your code (or the relevant parts). Hopefully you will get help from people more knowledgeable than me 🙂
Regards,
Bruno
Thanks Bruno 🙂
Hi Kjetil,
Thank you for sharing the steps.
I used the same steps to create it. Just adding few more details so that it is helpful for others.
For step 6, after you have read the file from Application server, pass the binary content in the parameter pt_content of the FM CVAPI_DOC_CHECKIN. Below is the sample code.
DATA : lv_utf TYPE xstring.
OPEN DATASET lv_file_path FOR INPUT IN BINARY MODE MESSAGE msg.
IF msg IS INITIAL.
CLEAR : lv_utf.
"Read the file from file path
READ DATASET lv_file_path INTO lv_utf.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_utf
IMPORTING
output_length = lv_bin_size
TABLES
binary_tab = lt_bin_table.
CLOSE DATASET lv_file_path.
ENDIF.
ls_doc_files-filename = iv_file_name.
ls_doc_files-storage_cat = iv_storage_category.
ls_doc_files-appnr = '1'.
ls_doc_files-dappl = iv_application.
ls_doc_files-description = iv_file_description.
ls_doc_files-checked_in = 'X'.
ls_doc_files-updateflag = 'I'.
APPEND ls_doc_files TO lt_doc_files.
LOOP AT lt_bin_table INTO DATA(ls_bin).
ls_drao-dokar = gv_doctype.
ls_drao-doknr = gv_docnumber.
ls_drao-dokvr = gv_docversion.
ls_drao-doktl = gv_docpart.
ls_drao-appnr = '1'.
ls_drao-zaehl = 1.
ls_drao-orln = lv_bin_size.
ls_drao-orblk = ls_bin-orblk.
APPEND ls_drao TO lt_drao.
CLEAR : ls_drao.
ENDLOOP.
ls_api_cntl-no_update_task = 'X'.
CALL FUNCTION 'CVAPI_DOC_CHECKIN'
EXPORTING
pf_dokar = gv_doctype
pf_doknr = gv_docnumber
pf_dokvr = gv_docversion
pf_doktl = gv_docpart
pf_ftp_dest = 'SAPFTPA'
pf_http_dest = 'SAPHTTPA'
ps_api_control = ls_api_cntl
pf_replace = 'X'
pf_content_provide = 'TBL'
IMPORTING
psx_message = ls_msg
TABLES
pt_files_x = lt_doc_files
pt_content = lt_drao.
.
IF ls_msg-msg_type CA 'EA'.
ev_return-type = ls_msg-msg_type.
ev_return-message = |Error when check-in the document|.
ROLLBACK WORK.
ELSE.
ev_return-type = 'S'.
ev_return-message = |Success|.
COMMIT WORK.
ENDIF.
Hi Bruno
Do you have code for email and attach dms documents.
Thnx in advance.
Regards
Omer
Hi Omer,
I do not, I'm sorry, and I don't have the time right now to look into that. But regarding the e-mail part look into standard class CL_BCS. Class CL_ZIP is also useful if you want to send the attachments in ZIP format.
Good luck and best regards,
Bruno
Thanks Bruno
I will look into it.
Regards
Omer
Hi Bruno,
Thanks a lot for the code. We do have a similar requirement. The documents from one system have to be migrated to another system.
Initially we are reading the DIR details using the FM "BAPI_DOCUMENT_GETDETAIL2 ".
Then converting the document files data to xstring using the FM's 'SDOK_PHIO_LOAD_CONTENT' and 'SCMS_XSTRING_TO_BINARY' .
Then I am saving the file to local system(Presentation server) using cl_gui_frontend_services=>gui_download . (This process of saving locally is to get the physical path for checking in the document using FM 'BAPI_DOCUMENT_CREATE2'
).
So far the above procedure is working fine as it is foreground. But now the requirement is to run report in background. I am sure the gui_download will not wotk in background. Alternatively has to use opendataset and closedataset in application server.
My query is, if I use the opendataset method here, how to use the SAPHTTPA and SAPFTPA .
Thanks in advance.
Siva
Hi Siva!
You are very welcome.
I also use the application server. The path to the file in the application server should go in fields "docpath" and "docfile" in the "documentfiles" table.
I guess the only difference from my scenario to your scenario, is that you don't use a VAULT to store the file in SAP. I would try a code similar to mine but without populating the storage category (line 18 on my code).
Give it a try... if it doesn't work, I guess you'll have to do a lot of debugging, like I did 🙂
Best regards,
Bruno
Hi Bruno,
Thanks for the reply.
I am able to save the file on application server and able to see in AL11 tcode. But when I am trying to checkin the document using the below FM.
CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
EXPORTING
documentdata = ls_doc
pf_ftp_dest = 'SAPFTPA'
pf_http_dest = 'SAPHTTPA'
IMPORTING
documenttype = ls_stb-dokar
documentnumber = ls_stb-doknr
documentpart = ls_stb-doktl
documentversion = ls_stb-dokvr
return = ls_return
TABLES
documentfiles = lt_documentfiles.
It is throwing the follow error.
Error while checking in and storing: /usr/sap/tmp/200372_001.TIF
Please help me on this issue.
Thanks,
Siva Krishna
.
Hi Siva,
Very hard for me to help you 🙁 If you'd like to share the entire code I could try to run it when I have some spare time (very rare these days) and see if I can find anything, but the SAP DMS can be configured in many different ways, so I'm pretty sure my results wouldn't be the same as yours.
My advice to you is to do the same as I did, get some coffee, some relaxing music, and debug the standard step by step.
Good luck,
Bruno
Hi Siva,
I am trying something similar. Lets say I have a doc no = 111. And it has 1 file as attachment.
I want to get that file in xstring format so that I can show that in my web dynpro(FPM) application.
How can I get the xstring format? Can you kindly detail it a bit.
Many many thanks i advance.
Regards,
Saikat
Hi, Bruno!
Thank you very much for sharing your code. It certainly helped me a lot!
BR,
Daniel pasinato
You are very welcome.
Best regards 🙂
Bruno
Hello Bruno,
nice example but i don't get the part where you are attaching file from application server.
I have my file on application server in directory DIR_HOME with path looks like F:\usr\sap\T60\..... and
ls_documentfiles-STORAGECATEGORY = 'DMS_C1_ST'.
ls_documentfiles-WSAPPLICATION = 'WW7'.
ls_documentfiles-DOCPATH = 'F:/usr/sap/T60/DVEBMGS01/work/'.
ls_documentfiles-DOCFILE = 'dms01.docx'.
ls_documentfiles-DESCRIPTION = 'DOCUMENT WORD 3'.
i get error: Error while checking in and storing:
F:/usr/sap/T60/DVEBMGS01/work/dms01.docx
So my question is you have in your code:
ls_files-docpath = '/tmp/'.
ls_files-docfile = 'dms_test4.pdf'.
So where is this file located?
Thanks
Miro
Hi Miro,
As far as I know, when you are accessing a file from the application server, you don't need the drive letter. So I think if you remove "F:" from the path, it should work.
I would troubleshoot this trying simple examples with the OPEN DATASET statement.
If you still have trouble, let me know, I can try to look further into this, but at the moment I'm a bit busy.
Best regards,
Bruno
Hi Bruno, the result after implementing your idea.
Do you have any other idea what to do? 🙂
Hi Miroslav,
Not really, no 😕
And even if I had the time to dig into it, my system is probably configured in a different way so I'm not sure if I would be able to replicate your issue.
My only advice to you is to do the same thing I did... many hours of debugging... 🙂
Cheers and good luck,
Bruno
Hi Bruno,
Great artcles.
I am trying something similar. Lets say I have a doc no = 111. And it has 1 file as attachment.
I want to get that file in xstring format so that I can show that in my web dynpro(FPM) application.
How can I get the xstring format? Can you kindly detail it a bit.
Many many thanks i advance.
Regards,
Saikat
Hi Saikat,
This reply is almost one year too late but maybe it will be helpful for others.
When you have your file in binary format (by using OPEN DATASET), you can use function module SCMS_BINARY_TO_XSTRING to convert it to XSTRING format.
Regards,
Bruno
Hi Bruno
Nice document.It helped me a lot. Even i am getting the same error like mirosalv and siva was talking. Any heads up for this error and how it could be solved. Error while checking in and storing: my file path. Thanks in advance.
Hi Burno
Nice document and it was very helpful and thanks for it first of all. As mirosalv and siva saying even I am getting the same error "Error While Checking in and storing: my file path". Any idea why I am getting this and how it could be solved if so please let me know and Thanks in advance.
Hi Jayadan,
As already replied to others, there are so many different ways that DMS can be configured, I'm not sure I'd be able to replicate your error. But looking at the error message, it looks like there might be an issue in the way the file name is being generated? Also, if you use it via dialog, are you able to store files? Could be missing some customizing.
Good luck,
Bruno
Hi Burno,
Nice knowledge sharing documents , I have work around it it help help to me upload document after refereing above document .
thanks Burno.
You're welcome, cheers!
Hi Bruno,
I read all comment in this blog and find very helpful for other scenario. My scenario is little different.
I have a program which runs daily to move all invoice attachment from GoS to Content server. In report i am using FM ARCHIVOBJECT_CREATE_TABLE and ARCHIV_CONNECTION_INSERT to move the attachment. After moving the attachment i am able to open the pdf document but not able to open word/excel file. content is getting corrupted and while opening the file it’s asking to convert. Please suggest if i am missing any config. I have configured the word file in DC30 tcode
Below is DC30 line for word document. One more point.. after adding entry in DC30 it started working but after EHP8 upgrade it’s not working. Please suggest.
WRD Word A2
Best Regards,
Srikanth
I don't think I'm able to help, my apologies. If you would post more information, such as the code where you make the conversions from one format to another, maybe someone with more experience in that specific scenario would be able to help you. One thing I would be extremely careful with, is the actual "zeros and ones" of the files. Make sure the underlying file system is similar and use raw data as much as possible.
Cheers.
Hi Bruno,
I have a small question on DMS..i am getting attachments from web service I see we need to store files temporarily(Desktop/Application server) if we use the FM
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
BAPI_DOCUMENT_CREATE2 as we have file path as one of the Inputs for FM.
is there any way we can avoid temporarily storing the files into the app server and can directly upload files to DMS..?
I really appreciate your help on this.
Thanks,
SarSAP.
Hi,
The application server is not a "front-end" desktop. This would be a huge problem. What I did, and I think this is mandatory, is to store the file in a temporary folder of the SAP system (application server), and after the file is checked-in to the vault server, I delete the temporary file.
So, to answer your question, I think the answer is no, you have to store the file temporarily in the application server, but there's no "desktop", the path should be something like /usr/sap/tmp or something like that.
Cheers,
Bruno
Hi Bruno,
I am able to successfully create a DIR with the document in application server. If I want to pick the document from another FTP location/my computer's local directory then what should i pass in the host name and how the RFC destination SAPFTPA should be configured. Please advice. Also how should we pass the below values in case of FTP/local directory.
Regards
IK
Hi,
I may be wrong, but I think you need to keep in mind that the BAPI that was used is basically a programmatic way of creating a document in SAP's DMS (or basically, the same as transaction CV01N). So I guess you need to configure the FTP location in SAP's DMS, but honestly I'm not an expert in this topic. I just stumbled upon a way of doing what I wanted to do, and shared in case someone has a similar requirement, but I can't really support people on this.
Cheers,
Bruno
Hi,
one of my first post, hopefully I will not be out of scope 🙂
I had a similar issue to create a Document info Record linked to an object and with files to be checked. The problem is that files are saved in an internal table as binaries and not stored somewhere on the server nor on the local PC of the user.
So I wrote that function module which fulfilled my requirements:
The function is quite simple as it creates first the DMS document is no document number is passed in structure "IS_DOCUMENT", the add new linked objects passed in table "IT_LINKS" and finally for each row of table IT_FILES, convert the xstring to a binary table, get required details default values like application type, etc... (which are part of the customizing of DMS).
Commits after each steps are required to avoid error like "Document XXX does not exist".
Hope it might help.
Thanks for that!
Maybe to make it clearer, you can create an independent blog post about it, and I can link to it on mine 🙂
Hi Bruno,
We created a program for uploading documents from application server. But the documents are not opening through transaction CV02N. We are getting the error message as shown below.
I think the file path is not recorded correctly in CV02N through the program. Only the file name is coming in the Original field.
The program code is as shown below.
Any suggestion on what could be the issue here?
Thanks in advance,
Rajeev
Hi All,
I have a small question on DMS.
While creating the document via FM 'BAPI_DOCUMENT_CREATE2' its goes into error "document category and documetn discpline required, we have customize the DMS as these two are always to be put as manadtory , how to handle this in progrm.
Please suggest.
Thanks & Regards,
Raj Singh
Hi Bruno,
Thank you for the details.
I also have a similar requirement and I followed the same steps as you have mentioned.
But I have an issue. I am able to create a DMS document with the file from the application server.
The problem is that it is empty file though the file has data in it.
Do you have any idea what might have gone wrong ? Do I have to pass anything more so that it picks the contents of the file ? Please suggest.
Regards,
Pavithra
Hi,
It has been too many years, sorry. Good luck.
Bruno