In this article, you will learn what are InStream and OutStream data types. How not to get confused by the naming and how to use them with some examples.

  • What Are InStream And OutStream?
  • The In And Out Confusing Directions
  • InStream Example
  • OutStream Example

What Are InStream And OutStream?


InStream and OutStream are data types that enable reading or writing to BLOB fields and files.

The information can travel thanks to these data types. We can think of them as connections. They connect to a data source to extract or insert information. The data source can be a Blob field, this is where the information is stored.

BLOBs rely heavily on these connections. If we want to master Blob handling we must learn about InStreams and OutStreams.

The In And Out Confusing Directions


The naming of InStream and OutStream can be confusing. If we try to think about ‘In’ as data coming into and ‘Out’ as data coming out of a Blob field we would be wrong.

The InStream data type allows reading information. Every time we want to read something we can think about InStreams.

On the other hand, OutStream allows writing to. When we need to modify a Blob field we will always use OutStream.

You can forget about the directions in and out, and relate InStream to reading and OutStream to writing.

InStream Example


One of the easiest way to use an InStream can be by reading a text from a local file.

First, uploading into the stream with the ‘UploadIntoStream‘ function. Then, reading the information from the stream. As you can see, the connection allows reading the information from a data source, which is the local file:


You can see another example of using InStream for uploading and downloading Media types in the following article:


You can get more information about InStream in the Microsoft documentation.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/instream/instream-data-type

OutStream Example


Usually, when we use OutStream for writing is because we will read the information at some point. For that reason, Outstream usually works together with InStream.

The following picture shows a simple example of how they can work together. We will use the ‘Temp Blob’ codeunit to store the information about a specific report.

First, we create the connection with ‘TempBlob.CreateOutStream‘. Now, the information is able to travel into the Blob. With ‘Report.SaveAs’ the information is stored in the Blob. Note that the OutStream is passed as a parameter.

After that, we are reading from the Blob with InStream.


In this video example you can see the Xml report generated:


The CopyStream function can be used to copy the content from an InStream into an OutStream.

In this example, we will modify the picture in ‘Company Information’. We are using an Instream to read from the local file. Then, with OutStream we create the connection to the picture field that we wish to modify or write into.

Finally, the information can travel from InStream to OutStream with CopyStream.


Video example:


Get more information about OutStream at the following link.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/outstream/outstream-data-type


Get more info about CopyStream in the Microsoft documentation below:

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/system/system-copystream-method


Source code:

codeunit 50010 "InStreamOutStreamMgmt"
{
    procedure InStrExample()
    var
        InStr: InStream;
        Description: Text;
        Text2: Text;
    begin
        UploadIntoStream('', '', '', Description, InStr);
        InStr.Read(Text2);
        Message(Text2);
    end;

    procedure ShowTop10CustomersInXml()
    var
        TempBlob: codeunit "Temp Blob";
        InStr: InStream;
        OutStr: OutStream;
        Content: Text;
    begin
        OutStr := TempBlob.CreateOutStream();
        Report.SaveAs(Report::"Customer - Top 10 List", '', ReportFormat::Xml, OutStr);
        InStr := TempBlob.CreateInStream();
        InStr.Read(Content);
        Message(Content);
    end;

    procedure UploadPictureToCompanyInfo()
    var
        CompanyInformation: Record "Company Information";
        InStr: InStream;
        OutStr: OutStream;
        ImageName: Text;
    begin
        CompanyInformation.Get();
        if UploadIntoStream('', '', '', ImageName, InStr) then begin
            CompanyInformation.Picture.CreateOutStream(OutStr);
            CopyStream(OutStr, InStr);
            CompanyInformation.Modify();
        end;
    end;
}



That´s all. Hope you find it helpful.

Master InStream And OutStream In Business Central

Post navigation


2 thoughts on “Master InStream And OutStream In Business Central

Leave a Reply

Your email address will not be published. Required fields are marked *