Search code examples
c#ssisabapsap-basis

download attachment from SAP using SO_DOCUMENT_READ_API1


I have a need to "download" the attachment from SAP PO using SSIS / C#.

Using the object SRGBTBREL

WHERE RELTYPE = 'ATTA'
AND CATID_A = 'BO'
AND LOGSYS_A = ''

I got a list of PO with attachmentkey (INSTID_B)

But I'm a little confuse to how using the SO_DOCUMENT_READ_API1 function

[...]
SO_DOCUMENT_READ_API1Request requestAttachment = new SO_DOCUMENT_READ_API1Request();
requestAttachment.DOCUMENT_ID = myAttachmentKey

I'm not familiar with SAP function in C#.

How can I manage to download the attachment file (or maybe got a link to the location of file) in SAP?

thanks in advance regards, Xavier


Solution

  • Finally found a solution: [...]

    SO_DOCUMENT_READ_API1Response responseAttachment = new SO_DOCUMENT_READ_API1Response();
    responseAttachment = RFCClient.SO_DOCUMENT_READ_API1(requestAttachment);
    
        SOLIX[] BinaryLines = responseAttachment.CONTENTS_HEX;
        ulong file_size;
        ulong.TryParse(responseAttachment.DOCUMENT_DATA.DOC_SIZE, out file_size);
        String FolderPath = DestinationPath;
        
        String path = FolderPath + "\\" + responseAttachment.DOCUMENT_DATA.OBJ_DESCR + "." + responseAttachment.DOCUMENT_DATA.OBJ_TYPE;
        FileStream fis = new FileStream(path, FileMode.Create);
    
        foreach (SOLIX Item in BinaryLines)
        {
            Byte[] bytes = Item.LINE;
            fis.Write(bytes, 0, bytes.Length);
            int rest = 255 - bytes.Length;
    
            if (rest < 1) continue;
        
            for (int i = 1; i<= rest; i++)
            {
                if ((ulong) fis.Length >= file_size)
                    break;
                fis.WriteByte(0);
    
            }
        }
        fis.Close();