You are on page 1of 7

1/28/2016 Printing or Downloading Service For Object Atta...

 | SCN

    Getting Started Newsletters Store

Hi, Guest Log On Join Us Search the Community

Products Services & Support About SCN Downloads


Activity Communications Actions
Industries Training & Education Partnership Developer Center

Lines of Business University Alliances Events & Webinars Innovation Browse

Printing or Downloading Service For Object Version 2

Attachments to local desktop
created by VIJAYKRISHNA GUDALA on Aug 18, 2013 8:22 AM, last modified by VIJAYKRISHNA GUDALA on Aug 18, 2013 11:31 AM

Share 0 Tweet

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

http://scn.sap.com/docs/DOC­45345 1/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
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

http://scn.sap.com/docs/DOC­45345 2/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
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

http://scn.sap.com/docs/DOC­45345 3/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
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

http://scn.sap.com/docs/DOC­45345 4/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
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

6306 Views   Categories: ABAP Development
Topics: abap Tags: download, print, objects, service, abap_objects, service_for_objects

Average User Rating

(6 ratings)

Share 0 Tweet

17 Comments

seshadri rajendra nath Aug 21, 2013 8:32 AM

Hai Vijay ,
 
Its useful document...
 
Thanks,
 
S.Rajendranath Raparthi.

Like (1)

VIJAYKRISHNA GUDALA Oct 22, 2013 9:18 AM (in response to seshadri rajendra nath)

Rajendra,
 
Thank you.

Like (0)

rajesh bethamcharla Oct 22, 2013 9:20 AM

Good one... Thanks for sharing...
 
Regards,
Rajesh

Like (1)

VIJAYKRISHNA GUDALA Oct 22, 2013 9:33 AM (in response to rajesh bethamcharla)

Rajesh, Thank you.

Like (0)

Modadugu Hemanth Kumar Oct 22, 2013 10:26 AM

Its clear and nice... thanks for sharing Vijaykrishna.
Like (1)

VIJAYKRISHNA GUDALA Oct 22, 2013 11:12 AM (in response to Modadugu Hemanth Kumar)

Thank you  Modadugu Hemanth Kumar

Like (0)

Hummel Christian Mar 6, 2014 11:34 AM

Hello,

http://scn.sap.com/docs/DOC­45345 5/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
 
i get
The field "I_CONNECTIONS_REC" is unknown, but there is a field with the similar name
"I_CONNECTIONS". "I_CONNECTIONS".
 
release 731.
 
how can i correct this? want to try if this report does what we need.
 
thanks

Like (0)

VIJAYKRISHNA GUDALA Mar 9, 2014 11:02 AM (in response to Hummel Christian)

Hi, please declare a structure (workarea) called I_CONNECTION_REC TYPE BDN_CON.
 
Regards,
Vijay

Like (0)

phanindra marreddy Mar 8, 2014 2:10 PM

Hi Vijay, good documentation. Thanks for sharing.
 
Regards,
Phani

Like (1)

Colm Gavin Feb 5, 2015 9:00 PM

Really useful, thanks!

Like (1)

seshadri rajendra nath Feb 10, 2015 7:10 AM

Very Very Helpful

Like (0)

Pradeep Gali Mar 12, 2015 6:10 PM

Very nice and clearly documented article

Like (1)

Suman P May 4, 2015 12:54 PM

Hi Vijay,
 
Thanks for sharing the info.
 
 
But I have the requirement which is slight different from the above.
 
I have to print multiple attachments from my report output with a button click, without saving
on the local machine. Please have a look at the below screen shot.

 
 
 
 
Can you please help me if you have any idea.

Like (0)

VIJAYKRISHNA GUDALA May 4, 2015 1:08 PM (in response to Suman P)

http://scn.sap.com/docs/DOC­45345 6/7
1/28/2016 Printing or Downloading Service For Object Atta... | SCN
Hi Suman,
 
have you tried above one. I think you will get all the attachments downloaded as it is in loop.
Try once.
 
Regards,
Vijay

Like (0)

Suman P May 4, 2015 1:13 PM (in response to VIJAYKRISHNA GUDALA)

Hi Vijay,
 
I tried the above one but it is not working. It should not download the attachments
in local machine and it should get print through the printer which is configured in
SAP server.
 
Do you any idea about this kind of scenario.
 
Thanks,
Suman

Like (0)

Suman P May 5, 2015 11:41 AM (in response to Suman P)

Hi Vijay,
 
I have tried the above one as one possible approach. It downloads in
SAP front­end workdir but print doesn't work. Can you please help in this.
 
Thanks,
Suman

Like (0)

satheesh kumar varma sagiraju Nov 19, 2015 10:58 AM (in response to VIJAYKRISHNA
GUDALA)

Hello Vijay,
 
can you me what parameters i need to pass in below FM  from
SO_OBJECT_READ
 
SO_OBJECT_DOWNLOAD by passing the component Id, Path and object content
fetched from above function module.

can you give me more details..

Like (0)

Site Index Contact Us SAP Help Portal


Follow SCN
Privacy Terms of Use Legal Disclosure Copyright

http://scn.sap.com/docs/DOC­45345 7/7

You might also like