...by Daniel Szego
quote
"On a long enough timeline we will all become Satoshi Nakamoto.."
Daniel Szego

Thursday, July 10, 2014

Reading out SharePoint list items from SAP ABAP

So, I know the usual way is to read out SAP data from SharePoint, but for the sake a variety let us see the reverse direction, reading out SharePoint List items and information from SAP. So, what you have to have is an SAP environment and a developer user. For example you can download and install one of the NetWeaver trial environment from scn.sap.com. Let we assume that we want to read out the elements of a SharePoint list called MyList, including 2 columns to read out : Title an​d PropertyX.


1. Log on to SAP

2. Start APAB Development workbench with /ose80

3. Add a new Program to the local objects called for instance     Z_SP_DEMO_GET_LIST_ITEM

4. Add local variable for managing urls :
   DATA: SEND_STRING    TYPE STRING,​   SEND_STRING1    TYPE STRING,
   SEND_STRING2   TYPE STRING.

5. Add local variables for managing result as string:
   DATA: w_string TYPE string,
      w_result TYPE string,      r_str TYPE string,w_result_trimmed TYPE string,      result_tab      
      TYPE TABLE OF string.

6. Add reference variable to http_client :
   DATA: http_client TYPE REF TO if_http_client.

7. To read out the elements of a list, you should use a REST web service from SharePoint _api/web/lists/GetByTitle() with special parameters to select column information of the items of the list. The two elements are set in SEND_STRING1 and SEND_STRING2 and they are concatenated in SEND_STRING.   ​
 SEND_STRING1 = 'http://sp13Dev/_api/web/lists/'.
 SEND_STRING2 ='GetByTitle(''MyList'')/items?      
    $select=Title,PropertyX'.
 CONCATENATE SEND_STRING1 SEND_STRING2 into 
    SEND_STRING.

8. Create the http object by create_by_url constructor:
  CALL METHOD cl_http_client=>create_by_url
   EXPORTING
    url = SEND_STRING
   IMPORTING
    client = http_client  
   EXCEPTIONS    argument_not_found = 1    plugin_not_active  = 2    
    internal_error     = 3
    others             = 4.
9. Disable authentification popup dialog :
  http_client->propertytype_logon_popup =        
     http_client->co_disabled.

10. Set Authentification : Username and password. (Please note that in this simple example the SharePoint authentification is set to basic and only on HTTP. More complicated and secure authentification mechanism can be imagined as well however they require additional configuration, like importing SSL certificate for HTTPS.)
   call method http_client->authenticate
   ( username = 
'username' PASSWORD = 'psw' ).​

11. Send the request :
  CALL METHOD http_client->send
   EXCEPTIONS
     http_communication_failure = 1         
     http_invalid_state         = 2.

12. Receive the result :
  CALL METHOD http_client->receive    
   EXCEPTIONS      
    http_communication_failure = 1         
    http_invalid_state         = 2        
    http_processing_failed     = 3.

13. Check if there was no error :
      if sy-subrc = 0.

14. Get the result xml as one big string into w_string:
   w_result = http_client->response->get_cdata( ).

15. The result at the moment is one huge xml file containing the Title and PropertyX column information as <d:Title> and <d:PropertyX> tags. You can either make some XML processing or just some simple ones to cut the necessary values of the two previously mentioned tags. We do in the following just a simple text transformation to cut out the basic values.​
   write: /'Title ', 'PropertyX'.
   REFRESH result_tab .
   SPLIT w_result AT '<' INTO TABLE result_tab .
     loop at result_tab into w_result.
       if w_result cs 'd:Title>' and not ( w_result cs
         '/d:Title' ) .          
           write :/ w_result+8 .       
        endif.      if w_result cs 'd:PropertyX>' and not ( w_result
         cs '/d:PropertyX'  ) . 
           write : w_result+12 .       
      endif.     
    endloop.    
  endif.

16. Check the program with CTRL F2 and then run with direct processing. If everything works without error you should get the list item values on the screen.



17. The end. You can celebrate, drink beer :)