Skip to Content
Author's profile photo Bruno Esperança

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

  1. DATA:
  2.       lv_docty  TYPE dokar,
  3.       lv_docnr  TYPE doknr,
  4.       lv_docpr  TYPE doktl_d,
  5.       lv_docvr  TYPE dokvr,
  6.       ls_ret    TYPE bapiret2,
  7.       lt_files  TYPE TABLE OF bapi_doc_files2,
  8.       ls_files  LIKE LINE OF lt_files,
  9.       ls_documentdata TYPE bapi_doc_draw2.
  10. CLEAR ls_documentdata.
  11. ls_documentdata-documenttype = 'ZCL'.
  12. ls_documentdata-description = 'Testing DMS'.
  13. ls_documentdata-username = sy-uname.
  14. CLEAR ls_files.
  15. ls_files-originaltype = '1'.
  16. ls_files-storagecategory = '<VAULT NAME HERE>'.
  17. ls_files-docpath = '/tmp/'.
  18. ls_files-docfile = 'dms_test4.pdf'.
  19. ls_files-wsapplication = 'PDF'.
  20. APPEND ls_files TO lt_files.
  21. CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
  22.   EXPORTING
  23.     documentdata    = ls_documentdata
  24.     pf_ftp_dest     = 'SAPFTPA'
  25.     pf_http_dest    = 'SAPHTTPA'
  26.   IMPORTING
  27.     documenttype    = lv_docty
  28.     documentnumber  = lv_docnr
  29.     documentpart    = lv_docpr
  30.     documentversion = lv_docvr
  31.     return          = ls_ret
  32.   TABLES
  33.     documentfiles   = lt_files.
  34. IF ls_ret-type NE 'E'.
  35.   CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  36.     EXPORTING
  37.       wait = abap_true.
  38. ELSE.
  39.   CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
  40. ENDIF.

And the result:

Display Document_ Basic Data Customer Claim (ZCL).png

I hope this will be helpful to someone!

Best regards,

Bruno

Assigned Tags

      55 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo MANGESH PANDE
      MANGESH PANDE

      Thanks Bruno.

      THis is very good code which can be used for various scenarios.

      With Warm Regards

      Mangesh Pande

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      Thanks, appreciate it!

      Bruno

      Author's profile photo P. M. Consultants
      P. M. Consultants

      what is the value of document type if we attach file for vendor(XK02/XK03)?

      Author's profile photo Kjetil Kilhavn
      Kjetil Kilhavn

      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?

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Kjetil Kilhavn
      Kjetil Kilhavn

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Kjetil Kilhavn
      Kjetil Kilhavn

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

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      Hi Kjetil!

      Any luck? I was hoping that my finding above could maybe help you "crack" the solution to your requirement.

      Cheers!

      Bruno

      Author's profile photo Kjetil Kilhavn
      Kjetil Kilhavn

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      Hi Kjetil,

      Interestingly enough I think I'm going to need this to. I am working on it now 😛

      Cheers,

      Bruno

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      So I'm glad I shared that piece of information about component controller LFS_SUST_FILEUPLOAD 😀

      Author's profile photo Kjetil Kilhavn
      Kjetil Kilhavn

      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 🙂

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      Thanks Bruno 🙂

      Author's profile photo Pavithra Meriyala
      Pavithra Meriyala

      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.

      Author's profile photo Former Member
      Former Member

      Hi Bruno

      Do you have code for email and attach dms documents.

      Thnx in advance.

      Regards

      Omer

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      Thanks Bruno

      I will look into it.

      Regards

      Omer

      Author's profile photo Former Member
      Former Member

      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   

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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

          .

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Daniel Pasinato
      Daniel Pasinato

      Hi, Bruno!

      Thank you very much for sharing your code. It certainly helped me a lot!

      BR,

      Daniel pasinato

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      You are very welcome.

      Best regards 🙂

      Bruno

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      Hi Bruno, the result after implementing your idea.

      DMS CHECKIN2 PROBLEM.png

      DMS CHECKIN2 PROBLEM2.png

      DMS CHECKIN2 PROBLEM3.png

      Do you have any other idea what to do? 🙂

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Jayadan Paramasivan
      Jayadan Paramasivan

      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.

      Author's profile photo Jayadan Paramasivan
      Jayadan Paramasivan

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo janak patel
      janak patel

      Hi Burno,

      Nice knowledge sharing documents , I have work around it it help help to me upload document after refereing above document .

      thanks Burno.

       

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      You're welcome, cheers!

      Author's profile photo Srikanth Akula
      Srikanth Akula

      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

       

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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.

      Author's profile photo Sar CRM
      Sar CRM

      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.

       

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

       

      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

      Author's profile photo Ibrahim Khan
      Ibrahim Khan

      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.

      1. ls_files-docpath = '/tmp/'.
      2. ls_files-docfile = 'dms_test4.pdf'.

       

       

      Regards

      IK

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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

      Author's profile photo Borner Frederic
      Borner Frederic

      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:

      FUNCTION z_fm_ca_set_docments_to_dms.
      *"----------------------------------------------------------------------
      *"*"Local Interface:
      *"  IMPORTING
      *"     VALUE(IS_DOCUMENT) TYPE  BAPI_DOC_DRAW2
      *"     VALUE(IT_LINK) TYPE  TT_BAPI_DOC_DRAD OPTIONAL
      *"     VALUE(IT_FILES) TYPE  ZCA_DMS_FILE_2_TT OPTIONAL
      *"  EXPORTING
      *"     VALUE(ET_RETURN) TYPE  BAPIRET2_TAB
      *"----------------------------------------------------------------------
        DATA: lt_objectlink     TYPE          tt_bapi_doc_drad,
              lt_objectlink_new TYPE          tt_bapi_doc_drad,
              lt_content        TYPE TABLE OF drao,
              lt_files          TYPE TABLE OF cvapi_doc_file,
              lt_comp_x         TYPE TABLE OF cvapi_doc_comp,
              lt_binary         TYPE TABLE OF orblk.
      
        DATA: ls_document TYPE bapi_doc_aux,
              ls_data     TYPE bapi_doc_draw2,
              ls_message  TYPE messages,
              ls_return   TYPE bapiret2.
      
        DATA: lv_file_size TYPE i.
      
        ls_data = is_document.
      
        IF ls_data-documenttype IS INITIAL.
          ls_data-documenttype    = zif_ps_cmt_m=>mc_dms_defaults-dms_type. "'GEN'
        ENDIF.
      
      * DMS document does not exist yet, create it.
        IF is_document-documentnumber  IS INITIAL AND
           is_document-documentpart    IS INITIAL AND
           is_document-documentversion IS INITIAL.
          CALL FUNCTION 'BAPI_DOCUMENT_CREATE2'
            EXPORTING
              documentdata    = is_document
              pf_ftp_dest     = zif_ca_constants=>gc_dms_ftp_rfc
              pf_http_dest    = zif_ca_constants=>gc_dms_http_rfc
            IMPORTING
              documenttype    = ls_document-doctype
              documentnumber  = ls_document-docnumber
              documentpart    = ls_document-docpart
              documentversion = ls_document-docversion
              return          = ls_return.
      *      TABLES
      *        objectlinks     = lt_objectlink.
      
          APPEND ls_return TO et_return.
      
          IF ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait = 'X'.
          ENDIF.
        ENDIF.
      
        lt_objectlink_new = it_link.
      
        IF NOT lt_objectlink_new IS INITIAL AND ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
          CLEAR: ls_return.
      
          CALL FUNCTION 'BAPI_DOCUMENT_GETOBJECTLINKS'
            EXPORTING
              documenttype    = ls_document-doctype
              documentnumber  = ls_document-docnumber
              documentpart    = ls_document-docpart
              documentversion = ls_document-docversion
            IMPORTING
              return          = ls_return
            TABLES
              objectlinks     = lt_objectlink.
      
          APPEND ls_return TO et_return.
      
      *   Only add links that do not exist yet.
          LOOP AT lt_objectlink REFERENCE INTO DATA(lr_link).
            DELETE lt_objectlink_new WHERE objectkey  = lr_link->objectkey
                                       AND objecttype = lr_link->objecttype.
          ENDLOOP.
      
          IF NOT lt_objectlink_new IS INITIAL AND ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
            CLEAR: ls_return.
      
            CALL FUNCTION 'BAPI_DOCUMENT_SAVEOBJECTLINKS'
              EXPORTING
                documenttype    = ls_document-doctype
                documentnumber  = ls_document-docnumber
                documentpart    = ls_document-docpart
                documentversion = ls_document-docversion
              IMPORTING
                return          = ls_return
              TABLES
                objectlinks     = lt_objectlink_new.
      
            APPEND ls_return TO et_return.
      
            IF ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = 'X'.
            ENDIF.
          ENDIF.
        ENDIF.
      
      * Add files to created document
        IF NOT it_files IS INITIAL AND ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
          LOOP AT it_files REFERENCE INTO DATA(lr_file).
            CLEAR: lt_binary, lv_file_size, lt_content, lt_files.
      
            CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
              EXPORTING
                buffer          = lr_file->rawfile
                append_to_table = abap_false
              IMPORTING
                output_length   = lv_file_size
              TABLES
                binary_tab      = lt_binary.
      
            LOOP AT lt_binary REFERENCE INTO DATA(lr_binary).
              APPEND INITIAL LINE TO lt_content ASSIGNING FIELD-SYMBOL(<fs_content>).
              <fs_content>-dokar = ls_document-doctype.
              <fs_content>-doknr = ls_document-docnumber.
              <fs_content>-dokvr = ls_document-docversion.
              <fs_content>-doktl = ls_document-docpart.
              <fs_content>-zaehl = sy-tabix.
              <fs_content>-orln  = lv_file_size.
              <fs_content>-orblk = lr_binary->*.
              DESCRIBE FIELD <fs_content>-orblk LENGTH <fs_content>-orbkl IN BYTE MODE.
            ENDLOOP.
      
            APPEND INITIAL LINE TO lt_files ASSIGNING FIELD-SYMBOL(<fs_file>).
            <fs_file>-dttrg          = ls_document-dttrg.
            <fs_file>-storage_cat    = lr_file->storagecategory.
      
            IF <fs_file>-storage_cat  IS INITIAL.
              <fs_file>-storage_cat  = zif_ca_constants=>gc_dms_default_storage. "'DMS_C1_ST'
            ENDIF.
      
            <fs_file>-langu          = sy-langu.
            <fs_file>-pathname       = lr_file->docpath.
      *      <fs_file>-filename       = lr_attachment->docfile.
            <fs_file>-description    = lr_file->description.
            <fs_file>-created_at     = lr_file->created_at.
            <fs_file>-created_by     = lr_file->created_by.
            <fs_file>-changed_at     = lr_file->changed_at.
            <fs_file>-changed_by     = lr_file->changed_by.
            <fs_file>-file_id        = lr_file->file_id.
            <fs_file>-dappl          = lr_file->wsapplication.
            <fs_file>-appnr          = lr_file->originaltype.
      
            IF <fs_file>-appnr IS INITIAL.
              <fs_file>-appnr = zif_ca_constants=>gc_dms_original_view_only. "'1'
            ENDIF.
      
            <fs_file>-active_version = abap_true.
      
            IF <fs_file>-dappl IS INITIAL.
      *       Get corresponding application type
              CALL FUNCTION 'CV120_DOC_GET_APPL'
                EXPORTING
                  pf_file   = lr_file->docfile
                IMPORTING
                  pfx_dappl = <fs_file>-dappl.
            ENDIF.
      
      
            CALL FUNCTION 'CVAPI_DOC_CHECKIN'
              EXPORTING
                pf_dokar           = ls_document-doctype
                pf_doknr           = ls_document-docnumber
                pf_dokvr           = ls_document-docversion
                pf_doktl           = ls_document-docpart
      *         PS_DOC_STATUS      =
                pf_ftp_dest        = zif_ca_constants=>gc_dms_ftp_rfc  "'SAPFTPA'
                pf_http_dest       = zif_ca_constants=>gc_dms_http_rfc "'SAPHTTPA'
      *         PF_HOSTNAME        = ' '
      *         PS_API_CONTROL     =
                pf_replace         = abap_true
                pf_content_provide = 'TBL'
              IMPORTING
                psx_message        = ls_message
              TABLES
                pt_files_x         = lt_files
      *         pt_comp_x          = lt_comp_x
                pt_content         = lt_content.
      
            CLEAR ls_return.
            MOVE-CORRESPONDING ls_message TO ls_return.
            APPEND ls_return TO et_return.
      
            IF ls_return-type NA /srmsmc/if_supplier_c=>gc_message_types_failure.
              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  wait = 'X'.
            ENDIF.
          ENDLOOP.
        ENDIF.
      ENDFUNCTION.

      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.

      Author's profile photo Bruno Esperança
      Bruno Esperança
      Blog Post Author

      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 🙂

      Author's profile photo Rajeev anandan
      Rajeev anandan

      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.

      REPORT  ztest_sample02.
      
      TYPES: BEGIN OF ts_document_hdr,
               type    TYPE dokar,
               number  TYPE doknr,
               part    TYPE doktl_d,
               version TYPE dokvr,
             END OF ts_document_hdr.
      
      DATA: ls_document_hdr  TYPE  ts_document_hdr,
            ls_documentdata  TYPE  bapi_doc_draw2,
            ls_documentdatax TYPE  bapi_doc_drawx2,
            ls_return        TYPE  bapiret2,
            ls_files         TYPE  bapi_doc_files2,
            lt_files         TYPE  STANDARD TABLE OF bapi_doc_files2.
      
      PARAMETERS:
        p_type      TYPE dokar    DEFAULT 'DMR',
        p_number    TYPE doknr    DEFAULT 'DMR100512     114D',
        p_part      TYPE doktl_d  DEFAULT '000',
        p_vers      TYPE dokvr    DEFAULT '01',
        p_path      TYPE dms_path DEFAULT '/tmp/',
        p_filnm     TYPE filep    DEFAULT 'doc.pdf',
        p_filty     TYPE dappl    DEFAULT 'PDF',
        p_datcar    TYPE dttrg    DEFAULT 'DV-1'.
      
      START-OF-SELECTION.
      
      * Document Header
        ls_document_hdr-type    = p_type.
        ls_document_hdr-number  = p_number.
        ls_document_hdr-part    = p_part.
        ls_document_hdr-version = p_vers.
      
      * Document Data
        ls_documentdata-wsapplication2  = p_filty.
        ls_documentdatax-wsapplication2 = abap_true.
        ls_documentdata-docfile2        = p_path && p_filnm.
        ls_documentdatax-docfile2       = abap_true.
      
      * Document Files
        ls_files-documenttype      = ls_document_hdr-type.
        ls_files-documentnumber    = ls_document_hdr-number.
        ls_files-documentpart      = ls_document_hdr-part.
        ls_files-documentversion   = ls_document_hdr-version.
        ls_files-docpath           = p_path.
        ls_files-docfile           = p_filnm.
        ls_files-description       = 'Filename ' && p_path && p_filnm.
        ls_files-wsapplication     = p_filty.
        ls_files-sourcedatacarrier = p_datcar.
        ls_files-originaltype      = '1'.
        APPEND ls_files TO lt_files.
      
      * Attach file to the Document.
        CALL FUNCTION 'BAPI_DOCUMENT_CHANGE2'
          EXPORTING
            documenttype    = ls_document_hdr-type
            documentnumber  = ls_document_hdr-number
            documentpart    = ls_document_hdr-part
            documentversion = ls_document_hdr-version
            documentdata    = ls_documentdata
            documentdatax   = ls_documentdatax
          IMPORTING
            return          = ls_return
          TABLES
            documentfiles   = lt_files.
      
        IF ls_return-type NE 'E'.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = abap_true.
          MESSAGE s000(zz) WITH 'Success'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' .
          MESSAGE ID ls_return-id TYPE ls_return-type NUMBER ls_return-number
                WITH ls_return-message_v1 ls_return-message_v2 ls_return-message_v3 ls_return-message_v4.
        ENDIF.

       

      Any suggestion on what could be the issue here?

       

      Thanks in advance,

      Rajeev

      Author's profile photo Raj Singh
      Raj Singh

      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

       

      Author's profile photo Pavithra Meriyala
      Pavithra Meriyala

      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

      Author's profile photo Bruno Esperanca
      Bruno Esperanca

      Hi,

      It has been too many years, sorry. Good luck.

      Bruno