For K2 connect developers, there comes a point in time where you want to return back binary data in SAP to the front end via SmartObjects.
Now the issue is that SAP data is binary whereas SmartObjects handle binary data in base64 encoded format. If you just map it directly it would not work as the formats are different.
Now this is where a transformation in the custom mapping feature becomes really handy. The transformation handler allows you to embed .NET code to manipulate the data within the mapping.
In this sample we are retrieving a PDF that was stored in SAP. We are mapping the File property in the SAP BAPI call to the PDF_DOC property in the service object.

The transformation code looks like this.
private void Transform (System.Byte[] PDF_DOC, ref System.String File)
{
byte[] bytes;
if (PDF_DOC.Length > 0)
{
long length = 0;
System.IO.MemoryStream ws = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
sf.Serialize(ws, PDF_DOC);
length = ws.Length;
bytes = ws.GetBuffer();
string encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
File = encodedData;
}
}
However, just be careful when using transformation code. This code executes for every record returned so this could potentially create a heavy load on your server if your number of records is large. However in certain cases, using a transformation code mapping is a very useful tool to keep in your code arsenal when you are faced with certain unique problems.
N.B. Credits to Ziyi for providing this solution.
Posted
Fri, Nov 6 2009 3:12 PM
by
johnny