Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
4,351

Hy everybody,

I recently had the requirement to provide a download URL for a document, that was stored in an Database Table of a SAP ERP System. The idea of the customer was, that the download link should be sent by mail. As i can not provide all tables and infos that are available in customer system, i've added a short section to get a document into a sap database table. At customer place, the documents were already stored in a table.

Now i would like to briefly describe the solution for interested people:
(Replace patterns like <value> with your proper values)

Basicly these steps are required.

Step 1) Get Document into Database Table

Step 2) Get Document out of the Database Table

Step 3) Download Document by SICF Service Implementation

Step 4) Add Link to E-Mail Text

Step 1) Get Document into Database Table:

First of all create a database table like this:

The database table is kept simple. Usually you would provide additional info like filetype and so on. Also the dataelements are simple.

Secondly create a simple upload report. You can find a sample report in the attachments (ZZTAB_UPLOAD.abap) (The file can be opened with Notepad or similar application).

Keep in Mind: The sample report is not designed for productive execution. Some Major requirements like error handling, coding style and so on are not properly implemented in this report. It's just a quick and really dirty solution to upload a file into the a database table. I'm sure you would be able to upgrade this report for your productive system, if you may be in need of it.

Try this example with a simple text file. As shown here it will not work for other file types eg. word/pdf documents. To achieve this, enhance the table with information about the uploaded file mime type & in the handler method set the corresponding mime type with following code:

server->response->set_header_field( name = 'Content-Type' value = 'application/msword' )

A list of supported mime-types can be found here: MimeTypes

Check your database table if you have your file properly uploaded by the report. Therefore just use SE16/SE16N to check the database entry.

Check Step 1): Finished -  A file is stored in a database table as XSTRING

Step 2) Get Document out of the Database Table

Basicly this step is pretty simple.

data: ls_dbtab type zztab_files.

select single * from zztab_files into ls_files where id = <id>.

if sy-subrc <> 0.

write 'error on select of document'

endif.

Check Step 2): Finished - A file is selected from a database table - But not downloaded using a SICF Service.

So now what to do with the selected file?

You could send it to the frontend like shown in the sample report using cl_gui_frontend_services. In a scenario where you're users are only using SAP GUI the solution would already fit (except for the coding issues mentioned above). Like mentioned in the introduction, i had a usecase where users needed to download the file by calling an url. Let us head to Step 3) now.

Step 3) Download Document by SICF Service Implementation

So what do we need? We need access to transaction SICF to create a service and activate it. We need to implement the http interface to send our selected file from the database to the frontend using a browser for example. So let's start.

First of all we need to create a class that implements the following interface IF_HTTP_EXTENSION. I assume that it's no problem to implement an interface - if there are questions about this, write me a pm or google is a good source achieving this. Implement the interface methods they can be empty by now, as we are coming to the implementation at a later point in this document.

Copy the class name into your clipboard as you require it shortly.

Call Transaction SICF & Execute it without any parameters. You should land in a view similiar to the following printscreen.

Create a new subelement at default_host/sap/bc node. There is a namespace warning appearing -> you can confirm the warning.

Now this screen appears: Enter a name for your service - i called it ZZTEST_FILE. Leave radiobuttons as they are set by default and click continue.

The following screen appears: Switch to the tab "Handler-List" and enter the class you just created in the handler list, add a meanigful description and save your changes.

The system will prompt you for package assignemt and a transport request. I've created them as local objects.

Hit the back button to get into the sicf tree overview. Search your service name unter default_host/sap/bc/<your_service_name> - Right Click on it and activate it. Confirm the the message and your service is up running. In the same context menu there is also an option "Test Service" keep this in mind and let transaction SICF open in a single mode of SAP GUI.

So in the transaction SICF now everything is set up for our file download. For security issues usually as developer i never had access to the SICF transaction and therefore always asked someone in the basis team to help out. They usually now SICF pretty well & can decide from a security viewpoint where the service has to be maintained. I'm sure they will help you out.

Let's get back to the Handler Class we created earlier. If you set a break-point at the endmethod statement of the method you can try if everything works by using the option Test Service in the SICF Context Menu on your service. Be sure to set an external breakpoint.

if you now test the service from SICF like mentioned above. You should end up in the debugger at the endmethod statement.

So now there is one single thing to do for Step 3) and that's implementing the handle_request method. I've provide the sample code also in the attachments.

Basicly the following things happen:

Read GET Parameters (as we will provide a GET Parameter fileid in the url call)

Check that only numeric values ar passed using the GET Parameter fileid / i want to prevent somebody executing sql via sql injection.

Select the file from the db tab

Set File Content in Response

Be aware, that in this context of programming lots of security aspects have to be covered. Thats why i strictly advise you to implement this example only on sandbox systems to get a basic understanding of the topic. If you want to use this approach on production => Improve the simple example drastically 

So now lets check the downloaded content:

Test your service using the SICF Test Service functionality described above.

There should be a browser starting & depending on your system settings your credentials will be prompted. Enter them. The following result should appear. I've hided the browser & unnecessary information but this should not disturb you.

You will see a URI with following pattern:

http(s)://<host>:<port>/sap/bc/zztest_file?sap-client=<yourclient>

The bold part in the url is exactly the navigation path in the SICF nodes. Then your service name is followed & by default sap-client parameter is also present.

To check your downloaded file append following GET parameter to your URI:

&fileid=<ID in DBTAB>

In my case it would be &fileid=4

So the complete URI for downloading a file is:

http(s)://<host>:<port>/sap/bc/zztest_file?sap-client=<yourclient>&fileid=4

Step 4) Sending the Link by E-Mail

This step is pretty easy using Business Document Classes. If you need help sending an E-Mail let me know and i'll complete this tutorial. By know i think there is plenty of good stuff available for sending mails with SAP.

I hope you enjoyed the tutorial and that there was something new for you and you have learned something. Have fun while giving it a try.

Best regards,

Michael

3 Comments
Labels in this area