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

To report this post you need to login first.

40 Comments

You must be Logged on to comment or reply to a post.

  1. 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?

    (0) 
    1. Bruno Esperança 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

      (0) 
      1. 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.

        (0) 
        1. Bruno Esperança 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

          (0) 
          1. 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. 😥

            (0) 
            1. Bruno Esperança 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_draoorblk = <fs>line.

                       ls_draoorln  = lv_len.

                       APPEND ls_drao TO lt_content.

                       ADD 1 TO ls_draozaehl.

              ENDLOOP.


              See if that helps!!


              Regards,

              Bruno

              (0) 
  2. 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 🙂

    (0) 
    1. krishna **

      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.

      (0) 
      1. Bruno Esperança 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

        (0) 
        1. krishna **

          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.

          (0) 
          1. Bruno Esperança 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

            (0) 
            1. krishna **

              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.

              (0) 
              1. Bruno Esperança 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

                (0) 
    1. Bruno Esperança 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

      (0) 
  3. siva krishna

    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   

    (0) 
    1. Bruno Esperança 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

      (0) 
      1. siva krishna

        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_stbdokar
            documentnumber             = ls_stbdoknr
            documentpart                   = ls_stbdoktl
            documentversion              = ls_stbdokvr
            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

            .

        (0) 
        1. Bruno Esperança 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

          (0) 
    2. Saikat Goswami

      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

      (0) 
  4. Miroslav Murin

    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_documentfilesSTORAGECATEGORY = ‘DMS_C1_ST’.

    ls_documentfilesWSAPPLICATION = ‘WW7’.

    ls_documentfilesDOCPATH = ‘F:/usr/sap/T60/DVEBMGS01/work/’.

    ls_documentfilesDOCFILE = ‘dms01.docx’.

    ls_documentfilesDESCRIPTION = ‘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_filesdocpath = ‘/tmp/’.

    ls_filesdocfile = ‘dms_test4.pdf’.



    So where is this file located?


    Thanks

    Miro

    (0) 
    1. Bruno Esperança 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

      (0) 
        1. Bruno Esperança 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

          (0) 
  5. Saikat Goswami

    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

    (0) 
    1. Bruno Esperança 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

      (0) 
  6. Jayadan .

    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.

    (0) 
  7. Jayadan .

    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.

    (0) 
    1. Bruno Esperança 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

      (0) 
  8. 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.

     

    (0) 

Leave a Reply