Thursday, July 4, 2024

Code to download the SSRS report to pdf in D365F&O X++

  public static void main(Args _args)

    {

        Filename                        fileName = "VendorRequest.pdf";

        SrsReportRunController          controller = new SrsReportRunController();

        SRSReportExecutionInfo          executionInfo = new SRSReportExecutionInfo();

        PRO_VendRequestContract         contract = new  PRO_VendRequestContract();

        Map                             reportParametersMap;

        SRSProxy                        srsProxy;

        System.IO.MemoryStream          mstream;

        SRSPrintDestinationSettings     settings;

        System.Byte[]                   reportBytes = new System.Byte[0]();

        SRSReportRunService             srsReportRunService = new SrsReportRunService();

        Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;

//normal code for calling report.

        PRO_VendRequestController   pRO_VendRequestController = pRO_VendRequestController::construct();

        pRO_VendRequestController.parmReportName(ssrsReportStr(PRO_VendRequest, Report));

        pRO_VendRequestController.parmArgs(_args);

        pRO_VendRequestController.parmShowDialog(false);

        pRO_VendRequestController.startOperation();


        //set controller parameters

        controller.parmReportName(ssrsReportStr(PRO_VendRequest, Report));//

        controller.parmShowDialog(false);

        controller.parmLoadFromSysLastValue(false);

        controller.parmReportContract().parmRdpContract(contract);


        // Provide printer settings

        settings = controller.parmReportContract().parmPrintSettings();

        settings.printMediumType(SRSPrintMediumType::File);

        settings.fileName(fileName);

        settings.fileFormat(SRSReportFileFormat::PDF);


        // Below is a part of code responsible for rendering the report

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

        controller.parmReportContract().parmReportExecutionInfo(executionInfo);


        //set proxy and service values

        srsReportRunService.getReportDataContract(controller.parmreportcontract().parmReportName());

        srsReportRunService.preRunReport(controller.parmreportcontract());

        reportParametersMap = srsReportRunService.createParamMapFromContract(controller.parmReportContract());

        parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

        srsProxy            = SRSProxy::constructWithConfiguration(controller.parmReportContract().parmReportServerConfig());


        // Actual rendering to byte array

        reportBytes = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),

                                          parameterValueArray,

                                          settings.fileFormat(),

                                          settings.deviceinfo());


        //conver the byte array to memory stream

        mstream         = new System.IO.MemoryStream(reportBytes);

        File::SendFileToUser(mstream, fileName);

    }

No comments:

Post a Comment