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: 
VijayaKrishnaG
Active Contributor

Many a times there is a business requirement of linking documents, entering notes, sending notes or linking an internet address to various SAP objects. These external attachments can be reference documents, pictures, Email attachments, designs, diagrams or related spreadsheets. To meet this requirement SAP has provided a tool bar called ‘Generic Object services toolbar’ or ‘GOS toolbar’.

For more info on Object Service (Service for objects) refer following link http://scn.sap.com/docs/DOC-33485

So, this document demonstrates how to download or print those attachments.

Though this Object service toolbar is available for many transactions, in this document I considered to download the attachments of functional locations. In this document I am creating a program which expects Functional Location and Download/Print option (Checkbox) and prints or downloads the Object service attachment of given functional location to a specified path in local desktop as output.

* SELECTION-SCREEN
PARAMETERS : P_FUNLOC TYPE ILOA-TPLNR,
              CB_PRVW 
TYPE C AS CHECKBOX.

Getting started, Step 1:

Get the Spool Requests that are generated by the Active User (user running the program) from the table TSP01 and hold the latest Spool by sorting and reading index 1.

TYPES : BEGIN OF LTY_TSP01,
RQIDENT  
TYPE RSPOID,
RQCRETIME
TYPE RSPOCRTIME,
RQFINAL  
TYPE RSPOFINAL,
END OF LTY_TSP01.

DATA : LI_TSP01 TYPE STANDARD TABLE OF LTY_TSP01,
LS_TSP01
TYPE LTY_TSP01.

*START-OF-SELECTION.
START-OF-SELECTION.

SELECT RQIDENT        " Spool request number
RQCRETIME     
" Time a spool request was created
RQFINAL       
" Spool request completed
FROM TSP01
INTO TABLE LI_TSP01
WHERE RQOWNER = SY-UNAME.

IF SY-SUBRC = 0.
SORT LI_TSP01 BY RQCRETIME DESCENDING.
CLEAR: LS_TSP01.

READ TABLE LI_TSP01 INTO LS_TSP01 INDEX 1.

Step 2:


Now modify the table TSP01 by updating the field - RQPRIO (Spool: Spool or print request priority) to 1 (Very high priority).

  IF SY-SUBRC = 0.
UPDATE TSP01
SET RQPRIO = '1'
WHERE RQIDENT = LS_TSP01-RQIDENT.

REFRESH LI_TSP01[].
ENDIF.
ENDIF.

Step 3:

Target any place in the system (desktop/presentation server) and get the list of files present in that directory.

To do so, call method DIRECTORY_LIST_FILES of class CL_GUI_FRONTEND_SERVICES passing directory path and get the count (no.of files exits) and list of files exists in the directory.

DATA : file_table TYPE STANDARD TABLE OF file_table,
COUNT      TYPE I,
LV_PATH   
TYPE STRING VALUE 'D:\usr\sap\WCM\Attachments\'.

"GET THE LIST OF FILES
CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES
EXPORTING
DIRECTORY                  
= LV_PATH
CHANGING
FILE_TABLE                 
= FILE_TABLE
COUNT                       = COUNT
EXCEPTIONS
CNTL_ERROR                 
= 1
DIRECTORY_LIST_FILES_FAILED
= 2
WRONG_PARAMETER            
= 3
ERROR_NO_GUI               
= 4
NOT_SUPPORTED_BY_GUI       
= 5
OTHERS                      = 6.

Step 4:

If the targeted directory is not empty (contains any files) after calling the above method, delete all those files and make directory empty by calling method FILE_DELETE of class CL_GUI_FRONTEND_SERVICES.

DATA : LV_FILENAME TYPE STRING,
RC         
TYPE I,
WA_LIST    
LIKE LINE OF FILE_TABLE.

IF FILE_TABLE[] IS NOT INITIAL.

LOOP AT FILE_TABLE INTO WA_LIST.

CONCATENATE LV_PATH WA_LIST-FILENAME INTO LV_FILENAME.
"DELETE THE EARLIER DOWNLOADED FILES
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_DELETE
EXPORTING
FILENAME            
= LV_FILENAME
CHANGING
RC                  
= RC
EXCEPTIONS
FILE_DELETE_FAILED  
= 1
CNTL_ERROR          
= 2
ERROR_NO_GUI        
= 3
FILE_NOT_FOUND      
= 4
ACCESS_DENIED       
= 5
UNKNOWN_ERROR       
= 6
NOT_SUPPORTED_BY_GUI
= 7
WRONG_PARAMETER     
= 8
OTHERS               = 9.
CLEAR: LV_FILENAME, RC.
ENDLOOP.
ENDIF.

Step 5:

Now get the Service for Object – Attachments by calling function BDS_GOS_CONNECTIONS_GET by passing the Class name (Business Document Service: Class name), Object key (Structure for Object ID), and client then get the attachments into an internal table.

DATA : I_CONNECTIONS TYPE STANDARD TABLE OF BDN_CON INITIAL SIZE 0,
L_OBJKEY     
TYPE SWOTOBJID-OBJKEY.

L_OBJKEY
= P_FUNLOC.

CALL FUNCTION 'BDS_GOS_CONNECTIONS_GET'
EXPORTING
CLASSNAME         
= 'BUS0010'
OBJKEY            
= L_OBJKEY
CLIENT             = SY-MANDT
TABLES
GOS_CONNECTIONS   
= I_CONNECTIONS
EXCEPTIONS
NO_OBJECTS_FOUND  
= 1
INTERNAL_ERROR    
= 2
INTERNAL_GOS_ERROR
= 3
OTHERS             = 4.
IF SY-SUBRC NE 0.
"DO NOTHING
ENDIF.  

Step 6:

Now get the object content by calling Function Module SO_OBJECT_READ by passing the Folder Id and Object Id captured from above function module.

Then download the Object (attachment) into the above targeted folder using function module SO_OBJECT_DOWNLOAD by passing the component Id, Path and object content fetched from above function module.

And also show Preview or directly print as per the user restrictions by calling method EXECUTE of class CL_GUI_FRONTEND_SERVICES by passing path of object and operation (either preview or print).

DATA : OBJCONT TYPE STANDARD TABLE OF SOLI INITIAL SIZE 0,
FOL_ID 
TYPE SOODK,
DOC_ID 
TYPE SOODK,

PATH   
TYPE CHAR255,
COMP_ID
TYPE CHAR255,

LV_MIN      
TYPE STRING,
LV_OPERATION
TYPE STRING,
I_PREVIEW   
TYPE TDPREVIEW.

IF CB_PRVW EQ 'X'.
I_PREVIEW
= 'X'.
ELSE.
I_PREVIEW
= SPACE.
ENDIF.

LOOP AT I_CONNECTIONS INTO I_CONNECTIONS_REC .

MOVE I_CONNECTIONS_REC-LOIO_ID TO FOL_ID .
MOVE I_CONNECTIONS_REC-LOIO_ID+17(25) TO DOC_ID .

CALL FUNCTION 'SO_OBJECT_READ'
EXPORTING
FOLDER_ID                 
= FOL_ID
OBJECT_ID                 
= DOC_ID
TABLES
OBJCONT                   
= OBJCONT
EXCEPTIONS
ACTIVE_USER_NOT_EXIST     
= 1
COMMUNICATION_FAILURE     
= 2
COMPONENT_NOT_AVAILABLE   
= 3
FOLDER_NOT_EXIST          
= 4
FOLDER_NO_AUTHORIZATION   
= 5
OBJECT_NOT_EXIST          
= 6
OBJECT_NO_AUTHORIZATION   
= 7
OPERATION_NO_AUTHORIZATION
= 8
OWNER_NOT_EXIST           
= 9
PARAMETER_ERROR           
= 10
SUBSTITUTE_NOT_ACTIVE     
= 11
SUBSTITUTE_NOT_DEFINED    
= 12
SYSTEM_FAILURE            
= 13
X_ERROR                   
= 14
OTHERS                     = 15.
IF SY-SUBRC NE 0.
"DO NOTHING
ENDIF.

CONCATENATE LV_PATH I_CONNECTIONS_REC-DESCRIPT
'.'
I_CONNECTIONS_REC
-DOCUCLASS
INTO PATH.

CONCATENATE I_CONNECTIONS_REC-DESCRIPT
'.'
I_CONNECTIONS_REC
-DOCUCLASS
INTO COMP_ID .

CALL FUNCTION 'SO_OBJECT_DOWNLOAD'
EXPORTING
DEFAULT_FILENAME
= COMP_ID
FILETYPE        
= 'BIN'
PATH_AND_FILE   
= PATH
EXTCT           
= 'K'
NO_DIALOG       
= 'X'
TABLES
OBJCONT         
= OBJCONT
EXCEPTIONS
FILE_WRITE_ERROR
= 1
INVALID_TYPE    
= 2
X_ERROR         
= 3
KPRO_ERROR      
= 4
OTHERS           = 5.
IF SY-SUBRC NE 0.
"DO NOTHING
ENDIF.

LV_FILENAME
= PATH.
IF I_PREVIEW = 'X'.
CLEAR: LV_MIN.
LV_OPERATION
= 'OPEN'.
ELSE.
LV_OPERATION
= 'PRINT'.
LV_MIN
= 'X'.
ENDIF.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE
EXPORTING
DOCUMENT              
= LV_FILENAME
MINIMIZED             
= LV_MIN
OPERATION             
= LV_OPERATION
EXCEPTIONS
CNTL_ERROR            
= 1
ERROR_NO_GUI          
= 2
BAD_PARAMETER         
= 3
FILE_NOT_FOUND        
= 4
PATH_NOT_FOUND        
= 5
FILE_EXTENSION_UNKNOWN
= 6
ERROR_EXECUTE_FAILED  
= 7
SYNCHRONOUS_FAILED    
= 8
NOT_SUPPORTED_BY_GUI  
= 9
OTHERS                 = 10.
ENDLOOP.

When I am working on this issue, I came across some documents related to this Service Objects but felt complex. So as I felt this the lesser code to achieve, I wrote this document. Any kind of suggestions or advices are accepted to Improve this.

Hope this will be helpful.

Thanks & Regard,

-Vijay

18 Comments
Labels in this area