DMS documents via HTTP service
Hi everyone,
A few weeks ago, I was looking for ways to get documents (pictures, PDF files) from SAP DMS to my iPhone. This might be possible via a direct connection between the content server and the iPhone. However, I didn't like exposing the content server in this way and was hoping to use some kind of Internet service in the R/3 system.
It turned out that this was quite easy to do with a custom REST service. It came with a nice bonus; I can also use the service in my browser and it will start the appropiate program (Acrobat Reader, Powerpoint, ...) as well. The last part depends on the correct configuration of DMS customizing, I will show you at the end of the blog how to do that.
The requirement
The requirement was quite simple. I want an URL, similar to the RFC via SOAP URLs, to which I can pass an HTTP request with the key of the DMS document. I want response to contain the (binary) content of the document. Documents in DMS can be identified uniquely using the four key fields:
- Number
(DRAW-DOKNR) - Version
(DRAW-DOKVR) - Part
(DRAW-DOKTL)
The simplest way of passing the document key is via an HTTP GET call, where the parameters are appended to the URL. So if the service URL is
http://<sapserver>:<port>/sap/bc/zdoc, the GET call is http://<sapserver>:<port>/sap/bc/zdoc?type=<type>&number=<number>&version=<version>&part=
Creating the service
You can create custom HTTP services in transaction SICF. In the selection screen, choose Execute (F8) and the following screen will appear:
Within the tree, you will find some frequently used services. For example, BSP pages can be found under path default_host/sap/bc/bsp/sap/, while WebDynpros reside under default_host/sap/bc/webdynpro/sap/. The path in the tree corresponds to the resulting URL of the service.
To create a new service, right-click on its parent and choose New Subelement from the context menu.
Dismiss the popup with a warning that SAP upgrades might overwrite the new service. A new popup appears where you must provide the name of the service:
After you choose Input (Enter), a screen appears where you can enter more information. At this moment, nothing is needed but you can enter a simple description. Choose Store (Ctrl + S) afterwards; because a service is a repository object you must either declare it as local or put it in a transport request.
When you go back, you can see the service in the tree. It is still gray because it isn't active yet.
Creating a handler
TYPE drao.
- 1.
IF lr_request->get_method( ) EQ 'GET'.
- 2.
- Retrieve document key
- 3.
- Check document existence and read details
- Put the error in the response
- 4.
- Convert file information
- Checkout document
- Put the error in the response
- 5.
- Put document content in response
- 6.
- Set MIME type
ENDMETHOD.
Some explanation of the code:
- The method
handle_requesthas one parameter calledserver. For us, only the HTTP request and the HTTP response are important. First of all, the HTTP method of the request is checked. - The document key is extracted from the URL. Note that the URL construct is regarded as a kind of form.
- The call of function module
CVAPI_DOC_GETDETAILserves two purposes: first, the existence of the document is checked; second, some of the details needed to view the document are retrieved. If there is an error, the error message is put into the body of the response (by methodset_cdata
- The document must be 'checked out' to an internal table, which is done by calling function module
CV120_DOC_CHECKOUT_VIEWwith the right parameters. Again, if there is an error, it is returned in the response. - The (binary) data in the internal table is put into a (binary) string of the correct length and put into the response. Note that we use the method
set_datafor binary data, instead ofset_cdatawhich is suitable for character data only.
!https://weblogs.sdn.sap.com/weblogs/images/252145204/2-010.PNG|alt=|src=https://weblogs.sdn.sap.com/weblogs/images/252145204/2-010.PNG!
This is an important step, because the browser will not be able to select the right program or plugin to open the file. Otherwise, you might see just some unintelligible text.
Assign the handler to the service
After activating the handler class, you must indicate that requests to the ZDOC service are handled by this class. Return to transaction SICF and open the service (hint: you can enter the service name on the selection screen). Select tab Handler List, enter the class name and choose Store (Ctrl + S).
Return to the previous screen and choose Activate Service from the context menu. You'll have to confirm this in the popup that appears.
After activation, you can test the service by choosing Test Service from the same context menu. A browser window will open, asking you for your SAP credentials.
Notice that the SAP client is also a parameter in the URL. If you omit it, the request will be processed by the default client. The service will return some error message because you didn't specify a document yet.
To perform a real test, lookup the key of some DMS document in transaction CV03N.
If you extend the URL in the address bar with
Comments