I was recently asked how someone could download all of the resumes (attachments) from an eRecruitment system. This could be a useful process performed by a HR administrator. I was sure there must be a simple solution, so I thought Google might provide a quick hit on the subject. What I found on google was that there are lots of other interested people asking the same question. I hope the code I'm providing here helps others out.
Philip Johnston
Full Source Code:
*&------------------------------------------------------------------------------------*
*& Report Z_RC_GET_ATTACHMENTS
*& Notes: Download Attachments to Local Storage on Hard Drive
*&------------------------------------------------------------------------------------*
*& Developed By: Philip Johnston
*& Date: 10/16/2012
*&------------------------------------------------------------------------------------*
report z_rc_get_attachments.
***************************************************************************************
*type-pools
***************************************************************************************
type-pools : slis.
***************************************************************************************
*types
***************************************************************************************
types: begin of gs_output,
plvar type plvar,
otype type otype,
objid type hrobjid,
aedtm type datum,
uname type usrname,
pernr type sobid,
att_type type rcf_attachment_type,
attachment type rcf_attachment,
partner type bu_partner,
name_first type bu_namep_f,
name_last type bu_namep_l,
file_name type hrkwf_description,
file_size type filesize,
new_name type string,
end of gs_output.
***************************************************************************************
*constants
***************************************************************************************
constants: true type boole_d value if_hrrcf_constants=>true,
gc_appl type hrkwf_application value 'RCF',
gc_doc_id type hrkwf_doc_id value 'ATTACHMENT'.
***************************************************************************************
*field symbols
***************************************************************************************
field-symbols: <fs_gt_table> type gs_output.
***************************************************************************************
*tables
***************************************************************************************
tables: hrp5134,
t77rcf_atttype.
***************************************************************************************
*data
***************************************************************************************
data: gv_repid like sy-repid,
gt_hrp5134 type table of hrp5134 with header line,
go_ex_root type ref to cx_root,
go_ex type ref to cx_hrrcf,
gt_out type table of gs_output,
gt_table like gt_out with header line,
gt_solix type solix_tab,
gt_fieldcat type slis_t_fieldcat_alv,
gs_layout type slis_layout_alv,
lo_ex type ref to cx_hrrcf,
lo_doc type ref to cl_hrkwf_doc,
ls_message type bapiret2,
ls_cand_hrobject type hrobject,
ls_attachment type p5134,
lo_att_bl type ref to cl_hrrcf_attachment_bl,
lo_instance type ref to cl_hrrcf_attachments,
lt_attachment type rcf_t_attachments_text,
ls_att_text type rcf_s_attachments_text,
lv_att_type type rcf_attachment_type,
lv_content type xstring,
lv_content_type type w3conttype,
lv_num_of_items type i,
lv_percentage type i,
lv_progress_text type string,
l_file_length type i,
l_file_with_path type string.
***************************************************************************************
*Begin First Selection Block
***************************************************************************************
selection-screen begin of block sel with frame title text-sel.
parameters gp_plvar type hrp5134-plvar no-display. "active plan variant
parameters: gp_otype type v_7rcf_atttype_u-otype obligatory. "otype
select-options gt_atype for t77rcf_atttype-att_typ. "attachment type
select-options gt_objid for hrp5134-objid. "object id
select-options gt_uname for hrp5134-uname. "user name
select-options gt_cdate for hrp5134-aedtm. "change date
selection-screen end of block sel.
***************************************************************************************
***************************************************************************************
*Begin Second Selection Block
***************************************************************************************
selection-screen begin of block inc with frame title text-exc.
selection-screen begin of line.
parameters: gp_excl type c radiobutton group mod1 default 'X' user-command mod2.
selection-screen: comment 4(70) text-exc for field gp_excl.
selection-screen end of line.
selection-screen begin of line.
parameters: gp_incl type c radiobutton group mod1.
selection-screen: comment 4(70) text-inc for field gp_incl.
selection-screen end of line.
selection-screen end of block inc.
***************************************************************************************
***************************************************************************************
*Begin Third Selection Block
***************************************************************************************
selection-screen begin of block mod with frame title text-vmd.
selection-screen begin of line.
parameters: gp_test type c radiobutton group mod2 default 'X' user-command mod2.
selection-screen: comment 4(70) text-tst for field gp_test.
selection-screen end of line.
selection-screen begin of line.
parameters: gp_fil type c radiobutton group mod2.
selection-screen: comment 4(70) text-fil for field gp_fil.
selection-screen end of line.
selection-screen end of block mod.
***************************************************************************************
***************************************************************************************
*Begin Fourth Selection Block
***************************************************************************************
selection-screen begin of block fil with frame title text-loc.
selection-screen begin of line.
parameters: p_ipath type string default 'c:\temp' obligatory.
selection-screen end of line.
selection-screen end of block fil.
***************************************************************************************
***************************************************************************************
*initialization
***************************************************************************************
initialization.
gv_repid = sy-repid.
***************************************************************************************
*at selection-screen output
***************************************************************************************
at selection-screen output.
if gp_fil is initial.
loop at screen.
if screen-name = 'I2'.
screen-input = '0'.
modify screen.
endif.
endloop.
endif.
***************************************************************************************
*at selection-screen on gp_otype
***************************************************************************************
at selection-screen on gp_otype.
if not gp_otype is initial.
call function 'RH_GET_ACTIVE_WF_PLVAR'
exporting
set_default_plvar = true
importing
act_plvar = gp_plvar
exceptions
no_active_plvar = 1
others = 99.
case sy-subrc.
when 0.
when others.
message e217(hrrcf0001).
endcase.
endif.
***************************************************************************************
*at selection-screen
***************************************************************************************
start-of-selection.
try.
select a~plvar a~otype a~objid a~aedtm a~uname a~att_type a~attachment
from hrp5134 as a
into corresponding fields of table gt_hrp5134
where a~plvar eq gp_plvar
and a~otype eq gp_otype
and a~att_type in gt_atype
and a~objid in gt_objid
and a~uname in gt_uname
and a~aedtm in gt_cdate.
sort gt_hrp5134 by otype objid att_type attachment ascending.
delete adjacent duplicates from gt_hrp5134 comparing otype objid att_type.
loop at gt_hrp5134.
move-corresponding gt_hrp5134 to gt_table.
append gt_table.
endloop.
call method cl_hrrcf_attachment_bl=>get_instance
receiving
return = lo_att_bl.
describe table gt_table lines lv_num_of_items.
loop at gt_table assigning <fs_gt_table>.
* update GUI progress information
perform show_progress_bar.
* get business partner information: bp number, bp first name, bp last name - also gets employee pernr if there is one
perform get_bp_number_and_name using <fs_gt_table>-objid changing <fs_gt_table>-partner <fs_gt_table>-name_first <fs_gt_table>-name_last <fs_gt_table>-pernr.
endloop.
* see if the user only wants records where the candidate has been hired
if not ( gp_excl is initial ).
sort gt_table by pernr att_type ascending.
delete gt_table where pernr is initial.
endif.
loop at gt_table assigning <fs_gt_table>.
* update GUI progress information
perform show_progress_bar.
* get business partner information: bp number, bp first name, bp last name - also gets employee pernr if there is one
* perform get_bp_number_and_name using <fs_gt_table>-objid changing <fs_gt_table>-partner <fs_gt_table>-name_first <fs_gt_table>-name_last <fs_gt_table>-pernr.
* build the key to our candidate
ls_cand_hrobject-plvar = gp_plvar.
ls_cand_hrobject-otype = <fs_gt_table>-otype.
ls_cand_hrobject-objid = <fs_gt_table>-objid.
* create an instance of attachments
call method cl_hrrcf_attachments=>get_instance
exporting
hrobject = ls_cand_hrobject
importing
instance = lo_instance.
* move the attachment key data we are searching for
move-corresponding <fs_gt_table> to ls_attachment.
try.
* read the binary content information and mime type
call method lo_instance->read_attachment
exporting
attachment_record = ls_attachment
importing
attachment_content = lv_content
attachment_mime = lv_content_type.
call function 'SCMS_XSTRING_TO_BINARY'
exporting
buffer = lv_content
importing
output_length = l_file_length
tables
binary_tab = gt_solix.
* search doc on KW to get the actual filename
call method cl_hrkwf_doc=>get_doc
exporting
application = gc_appl
document_id = gc_doc_id
object_id = ls_cand_hrobject-objid
object_type = ls_cand_hrobject-otype
plvar = ls_cand_hrobject-plvar
version = <fs_gt_table>-attachment
receiving
doc = lo_doc.
<fs_gt_table>-file_name = lo_doc->description.
<fs_gt_table>-file_size = l_file_length.
case <fs_gt_table>-att_type.
when '0001'.
concatenate <fs_gt_table>-uname '_Resume_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
when '0002'.
concatenate <fs_gt_table>-uname '_CoverLetter_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
when '0005'.
concatenate <fs_gt_table>-uname '_Certification_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
when '0007'.
concatenate <fs_gt_table>-uname '_Other_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
when '0008'.
concatenate <fs_gt_table>-uname '_References_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
when others.
concatenate <fs_gt_table>-uname '_OtherFileType_' <fs_gt_table>-file_name into <fs_gt_table>-new_name.
endcase.
if gp_test is initial.
* user has requested to download files
concatenate p_ipath '\' <fs_gt_table>-new_name into l_file_with_path.
call method cl_gui_frontend_services=>gui_download
exporting
bin_filesize = l_file_length
filename = l_file_with_path
filetype = 'BIN'
changing
data_tab = gt_solix
exceptions
others = 3.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endif.
catch cx_hrrcf into go_ex.
<fs_gt_table>-file_name = '<no file found>'.
<fs_gt_table>-file_size = 0.
endtry.
endloop.
perform build_fieldcat_alv changing gt_fieldcat.
perform set_layout_alv changing gs_layout.
perform display_alv.
catch cx_root into go_ex_root.
call method cl_hrrcf_exception_handler=>write_exception_log( go_ex_root ). "analyse with SLG1
message e000(hrrcf0001).
endtry.
end-of-selection.
*&---------------------------------------------------------------------*
*& Form build_fieldcat_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CT_FIELDCAT text
*----------------------------------------------------------------------*
form build_fieldcat_alv changing ct_fieldcat type slis_t_fieldcat_alv.
data: ls_fieldcat like line of ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'PLVAR'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'PLVAR'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'OTYPE'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'OTYPE'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'OBJID'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'OBJID'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'AEDTM'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'AEDTM'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'UNAME'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'UNAME'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'PERNR'.
ls_fieldcat-ref_tabname = 'HRP5580'.
ls_fieldcat-ref_fieldname = 'PERNR'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'ATT_TYPE'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'ATT_TYPE'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = 'X'.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'ATTACHMENT'.
ls_fieldcat-ref_tabname = 'HRP5134'.
ls_fieldcat-ref_fieldname = 'ATTACHMENT'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = 'X'.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'PARTNER'.
ls_fieldcat-ref_tabname = 'BUT000'.
ls_fieldcat-ref_fieldname = 'PARTNER'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = 'X'.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'NAME_FIRST'.
ls_fieldcat-ref_tabname = 'BUT000'.
ls_fieldcat-ref_fieldname = 'NAME_FIRST'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'NAME_LAST'.
ls_fieldcat-ref_tabname = 'BUT000'.
ls_fieldcat-ref_fieldname = 'NAME_LAST'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'FILE_NAME'.
ls_fieldcat-ref_tabname = ' '.
ls_fieldcat-ref_fieldname = 'FILE_NAME'.
ls_fieldcat-seltext_m = 'Original File Name'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'FILE_SIZE'.
ls_fieldcat-ref_tabname = ' '.
ls_fieldcat-ref_fieldname = 'FILE_SIZE'.
ls_fieldcat-seltext_m = 'File Size (Bytes)'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'C'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
clear ls_fieldcat.
ls_fieldcat-fieldname = 'NEW_NAME'.
ls_fieldcat-ref_tabname = ' '.
ls_fieldcat-ref_fieldname = 'NEW_NAME'.
ls_fieldcat-seltext_m = 'New File Name'.
ls_fieldcat-edit = ' '.
ls_fieldcat-just = 'L'.
ls_fieldcat-no_out = ' '.
ls_fieldcat-lzero = ' '.
append ls_fieldcat to ct_fieldcat.
endform. "build_fieldcat_alv
*&---------------------------------------------------------------------*
*& Form set_layout_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CS_LAYOUT text
*----------------------------------------------------------------------*
form set_layout_alv changing cs_layout type slis_layout_alv.
cs_layout-window_titlebar = 'Candidate Attachment Download'.
cs_layout-colwidth_optimize = 'X'.
endform. "set_layout_alv
*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display_alv raising cx_hrrcf.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = gv_repid
is_layout = gs_layout
it_fieldcat = gt_fieldcat
i_save = 'A'
tables
t_outtab = gt_table
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. "display_alv
*&---------------------------------------------------------------------*
*& Form GET_BP_NUMBER_AND_NAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_<FS_GT_TABLE>_PARTNER text
* <--P_<FS_GT_TABLE>_NAME_FIRST text
* <--P_<FS_GT_TABLE>_NAME_LAST text
*----------------------------------------------------------------------*
form get_bp_number_and_name using p_fs_gt_table_objid
changing p_fs_gt_table_partner
p_fs_gt_table_name_first
p_fs_gt_table_name_last
p_fs_gt_table_pernr.
select single b~sobid c~name_first c~name_last d~sobid into (p_fs_gt_table_partner, p_fs_gt_table_name_first, p_fs_gt_table_name_last, p_fs_gt_table_pernr)
from hrp1001 as a
inner join hrp1001 as b on a~objid = b~objid
and b~plvar eq gp_plvar
and b~istat eq '1'
and b~otype eq 'CP'
and b~rsign eq 'B'
and b~relat eq '207'
and b~endda eq '99991231'
inner join but000 as c on b~sobid = c~partner
left outer join hrp1001 as d on a~objid = d~objid
and d~plvar eq gp_plvar
and d~istat eq '1'
and d~otype eq 'CP'
and d~rsign eq 'B'
and d~relat eq '209'
and d~endda eq '99991231'
where a~sobid = p_fs_gt_table_objid and
a~plvar eq gp_plvar and
b~istat eq '1' and
a~otype = 'CP' and
a~rsign = 'B' and
a~relat = '650' and
a~begda le sy-datum and
a~endda ge sy-datum.
if sy-subrc ne 0.
endif.
endform. " GET_BP_NUMBER_AND_NAME
*&---------------------------------------------------------------------*
*& Form SHOW_PROGRESS_BAR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form show_progress_bar .
lv_percentage = ( sy-tabix * 100 ) / lv_num_of_items.
lv_progress_text = lv_percentage.
concatenate lv_progress_text '% processing completed!!!' into lv_progress_text.
condense lv_progress_text.
call function 'SAPGUI_PROGRESS_INDICATOR'
exporting
percentage = lv_percentage
text = lv_progress_text.
endform. " SHOW_PROGRESS_BAR
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
6 | |
4 | |
4 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |