Generate SSRS Report as Byte Array

If you need to generate a SSRS report as a byte array, you can use a utility X++ class we created for some custom scenarios such as sending Sales and Project invoice in the single email to the corresponding customer. We will explain this particular scenario in our next article.

The helper class for generation SSRS reports in memory is named DocSrsReportGenerator and is part of Docentric Free Edition.

Download Free Edition >>

Generate Non-Print management report

Let’s take a look how we can generate the content of a Non-Print management report such as Number of workers using DocSrsReportGenerator, a helper class for generating SSRS reports in memory, i.e. as D365FO containers.

We can use the same class, DocSrsReportGenerator, to generate reports using Docentric templates and Docentric rendering engine as well.

// Generate Docentric report using Default template.
container generatedReport =
    new DocSrsReportGenerator(controller).generateDocentricReport(DocOutputFileFormat::DOCX);

In Docentric Free Edition you will also find useful utility methods, e.g. for converting D365FO containers to .NET byte arrays or memory streams.
// Convert container to a byte array.
System.Byte[] generatedReportByteArray =
    DocGlobalHelper::convertContainerToBytes(generatedReport);

// Convert container to a memory stream.
System.IO.MemoryStream generatedReportMemoryStream =
    DocGlobalHelper::convertContainerToMemoryStream(generatedReport);

Generate Print management report

We will now demonstrate how to generate a Print management report as a byte array on the example of Sales invoice. This code is a bit more complex, since we have to involve the SalesInvoiceJournalPrint class in order to avoid printing using Print management.

How DocSrsReportGenerator works

The key method in the DocSrsReportGenerator class is startOperationSynchronouslyAndGetReportContent(), in which the report controller is configured to run synchronously, without showing the report dialog and loading parameters from SysLastValue.

At the beginning of the method we subscribed to the renderingCompleted event, so when controller.startOperation() is invoked, it will block the execution until the renderingComplete event handler completes.

Note that in the renderingComplete event handler we will store the reference to the report content into an internal field, reportContentAsContainer. This is possible because we extended the SRSReportExecutionInfo object to contain the generated report content.

As mentioned earlier, in the renderingComplete event handler we can access to the generated document using the SRSReportExecutionInfo object extended with Docentric properties. In fact, to generate report as a byte array, we used a hidden Docentric print destination called Memory, whose purpose is just that – to generate a report in memory in the wanted output format.

You can use the DocSrsReportGenerator class with both SSRS and Docentric designs and rendering engines to generate reports in memory instead of distributing them to print destinations such as Email, Printer or File. By using this simple class, you can achieve your custom reporting scenarios more easily.

If you find the DocSrsReportGenerator class handy, just download Docentric Free Edition and start using it!

In our next article, you can see how to use it to send Sales and Project invoice in the single email.
Read the article >>

Leave a Reply

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

*

Docentric respects your privacy. Learn how your comment data is processed >>

Docentric respects your privacy. Learn how your comment data is processed >>