Skip to Content
Author's profile photo Yan Wong

A small working example of how to use the Abap webdav client to write files

In this example, I will show how to use the classes in package SWEBDAV_CLIENT to write document to a webdav server. The targeted webdav server is a Livelink server.

 

First, open a connection using the class cl_swdcl_connection static method create_by_user:

      lo_connection = cl_swdcl_connection=>create_by_user(
        i_server     
= p_host
        i_user       
= p_unam
        i_password   
= p_pwd
     
).

You can also use static method create_by_destination if you prefer to use a HTTP connection stored in SM59, which is much more secure.

 

After the connection is created, use the class cl_swdcl_client to get a client instance to webdav server:

 

      lo_client = cl_swdcl_client=>get_instance( lo_connection ).

 

The verb to write documents on a webdav server is PUT. It is an operation on the namespace. For that, I’ll use class cl_swdcl_namespace_client, it is bound to the webdav client instance:

     lo_namespace = cl_swdcl_namespace_client=>create( lo_client ).

I then use method WRITE_CONTENT to write the document in the Webdav server:

      lo_namespace->write_content(
          i_url           
= p_url
          i_lock_token    
= ‘TEST’
          i_content_type  
= ‘text/plain’
          i_content       
= l_content ).

 

Mandatory parameters are URL, content type and content. The content must be a Xstring value. Content converter to Xstring are mandatory before using the method (eg function module TREX_TEXT_TO_XSTRING). The lock is optional and only intended if you want to reserve a document. The content type is the mime type.

Parameters for the report

/wp-content/uploads/2013/10/1_297562.png

After execution of the report, the result on the Livelink server:

/wp-content/uploads/2013/10/2_297584.png

Content of the file:

/wp-content/uploads/2013/10/3_297585.png

Full source code of the report for this example:

REPORT  ztest_webdav.

PARAMETERS:
p_host
TYPE string DEFAULT http://doclinkLOWER CASE ,
p_url 
TYPE string DEFAULT ‘/livelinkdav/nodes/31824299/Hello%20Word.txt’ LOWER CASE,
p_unam
TYPE string DEFAULT ‘<Webdav user name>’ ,
p_pwd 
TYPE string LOWER CASE.

DATA:
  lo_connection
TYPE REF TO if_swdcl_connection,
  lo_client    
TYPE REF TO if_swdcl_client,
  lo_namespace 
TYPE REF TO if_swdcl_namespace_client,
  l_content    
TYPE xstring,
  lo_error     
TYPE REF TO cx_root.

START-OF-SELECTION.
 
TRY.
      lo_connection
= cl_swdcl_connection=>create_by_user(
        i_server     
= p_host
        i_user       
= p_unam
        i_password   
= p_pwd

      ).

      lo_client = cl_swdcl_client=>get_instance( lo_connection ).

      lo_namespace = cl_swdcl_namespace_client=>create( lo_client ).

      CALL FUNCTION ‘TREX_TEXT_TO_XSTRING’
       
EXPORTING
         
text   = ‘Hello World’
       
IMPORTING
         
buffer = l_content
       
EXCEPTIONS
         
OTHERS = 0.

      lo_namespace->write_content(
          i_url           
= p_url
          i_lock_token    
= ‘TEST’
          i_content_type  
= ‘text/plain’
          i_content       
= l_content ).
   
CATCH cx_root INTO lo_error.
     
MESSAGE lo_error TYPE ‘E’.
 
ENDTRY.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.