This sample shows how to upload documents to the BDS (Business Document Service) using the function module BDS_BUSINESSDOCUMENT_CREA_TAB.
About
Upload documents like excel sheets, word documents or images to the BDS by reading the file into a byte array and sending the array line after line to the function module BDS_BUSINESSDOCUMENT_CREA_TAB.
The depicted example uploads an excel sheet to the BDS. With small code changes to the document class, the document and MIME-type you can upload other document types.
Tip
You can look up the relevant document classes and types in SAP IMG > Basis Services > Archive Link > Basic Customizing.
In the sample code the class name (BKPF) represents the Accounting document type. You can find other classes in the Document Navigator (TA OAOR). The object key (100000000115642009) for BKPF is build from the company code, document number and the fiscal year.
Upload an Excel File to the BDS
The following sample code uploads an excel sheet to the BDS:
staticvoidMain(string[]args){try{stringsPath=System.IO.Path.GetDirectoryName(@"C:\temp\Material.xlsx");stringsFile=System.IO.Path.GetFileName(@"C:\temp\Material.xlsx");byte[]bytes;R3Connectioncon=newR3Connection("ptmalg",05,"xxx","xxx","en","800");con.Open(false);RFCFunctionfunc=con.CreateFunction("BDS_BUSINESSDOCUMENT_CREA_TAB");RFCTabletbData=func.Tables["CONTENT"];RFCTabletbSig=func.Tables["SIGNATURE"];RFCTabletbComp=func.Tables["COMPONENTS"];func.Exports["CLASSNAME"].ParamValue="BKPF";func.Exports["CLASSTYPE"].ParamValue="BO";func.Exports["OBJECT_KEY"].ParamValue="100000000115642009";func.Exports["BINARY_FLAG"].ParamValue="X";bytes=System.IO.File.ReadAllBytes(@"C:\temp\Material.xlsx");RFCStructureContent=newRFCStructure();for(intoffset=0;offset<bytes.Length;offset+=1022){byte[]ExportBytes=newbyte[1022];// In VB change the value of this array to 1021if(offset+1022>bytes.Length)Array.Copy(bytes,offset,ExportBytes,0,bytes.Length-offset);elseArray.Copy(bytes,offset,ExportBytes,0,1022);Content=tbData.AddRow();Content["LINE"]=ExportBytes;}RFCStructurerecComp=tbComp.AddRow();recComp["DOC_COUNT"]=1;recComp["COMP_COUNT"]=1;recComp["COMP_ID"]=sFile;recComp["MIMETYPE"]="application/vnd.ms-excel";recComp["COMP_SIZE"]=bytes.Length;RFCStructurerecSig=tbSig.AddRow();recSig["DOC_COUNT"]=1;recSig["DOC_ID"]="";recSig["DOC_VER_NO"]=1;recSig["DOC_VAR_ID"]=1;recSig["DOC_VAR_TG"]="OR";recSig["COMP_COUNT"]=1;recSig["PROP_NAME"]="BDS_DOCUMENTTYPE";recSig["PROP_VALUE"]="BDS_SHEET";recSig=tbSig.AddRow();recSig["DOC_COUNT"]=1;recSig["DOC_ID"]="";recSig["DOC_VER_NO"]=1;recSig["DOC_VAR_ID"]=1;recSig["DOC_VAR_TG"]="OR";recSig["COMP_COUNT"]=1;recSig["PROP_NAME"]="BDS_DOCUMENTCLASS";recSig["PROP_VALUE"]="XLS";recSig=tbSig.AddRow();recSig["DOC_COUNT"]=1;recSig["DOC_ID"]="";recSig["DOC_VER_NO"]=1;recSig["DOC_VAR_ID"]=1;recSig["DOC_VAR_TG"]="OR";recSig["COMP_COUNT"]=1;recSig["PROP_NAME"]="DESCRIPTION";recSig["PROP_VALUE"]=sFile;recSig=tbSig.AddRow();recSig["DOC_COUNT"]=1;recSig["DOC_ID"]="";recSig["DOC_VER_NO"]=1;recSig["DOC_VAR_ID"]=1;recSig["DOC_VAR_TG"]="OR";recSig["COMP_COUNT"]=1;recSig["PROP_NAME"]="LANGUAGE";recSig["PROP_VALUE"]="EN";func.Execute();Console.WriteLine("Upload OK");Console.ReadLine();}catch(Exceptione1){Console.WriteLine(e1.Message);Console.ReadLine();}}