Thursday, June 16, 2016

Writing SSRS report directly to file / stream / byte array on AX7

If you ever need to write a report directly to a file, or for some reason get access to it before it is sent to the user, then you can replace your call to runReport on your SrsReportRun instance (similar code as when writing a report from code on AX 2012) with:

reportRun.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());

SrsReportRunService reportRunService = new SrsReportRunService();
reportRunService.parmLocalMode(false);

reportRunService.preRunReport(reportRun.parmReportContract());


Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;
Map reportParametersMap;
reportParametersMap = reportRunService.createParamMapFromContract(reportRun.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);
SrsReportRunPrinter printer = new SrsReportRunPrinter(reportRun.parmReportContract(), parameterValueArray);

printer.parmSrsProxy().parmReportExecutionInfo(executionInfo);

System.Byte[] reportBytes = printer.parmSrsProxy().renderReportToByteArray(reportRun.parmReportContract().parmReportPath(),
                                          parameterValueArray,
                                          SRSReportFileFormat::PDF,
                                          printSettings.deviceInfo());

System.IO.File::WriteAllBytes("c:\\temp\\test.pdf", reportBytes);

If you need a stream instead, it should be trivial to create one with the byte array using the .net framework.