Sunday, March 24, 2024

Upload SSRS to blob storage

In this blog of DynamicsCommunity101 we will learn how to generate SSRS reports via code and upload them to blob storage

InventParameters    InventParametersLocal;

        select firstonly InventParametersLocal;


        if(InventParametersLocal.LSAttachmentsToAzureBlobStorage)

        {

            InventJournalTable  inventJournalTableLocal;

            select firstonly inventJournalTableLocal

                where inventJournalTableLocal.RecId == _recId;

            if(inventJournalTableLocal.WorkflowApprovalStatus == InventJournalWorkflowApprovalStatus::Submitted)

            {

                LSInventJournalTransPAContract          poContract;

                LSInventJournalTransPAController        controller  = new LSInventJournalTransPAController();

                SRSPrintDestinationSettings             settings;

                Filename                                fileName    = "Movement Journal Lines - " + inventJournalTableLocal.JournalId + ".pdf";

                controller.parmReportName(ssrsReportStr(LSInventJournalTransPA, Report));

                controller.parmShowDialog(false);

                controller.parmLoadFromSysLastValue(false);

    

                poContract          = controller.parmReportContract().parmRdpContract();

                poContract.parmInventJournalId(inventJournalTableLocal.JournalId);

        

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

                SRSProxy srsProxy;

                SRSReportRunService srsReportRunService = new SrsReportRunService();

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

                Map reportParametersMap;

                SRSReportExecutionInfo executionInfo = new SRSReportExecutionInfo();

                ;

                controller.parmReportName(ssrsReportStr(LSInventJournalTransPA, Report));

                controller.parmShowDialog(false);

                controller.parmLoadFromSysLastValue(false);

                controller.parmReportContract().parmRdpContract(poContract);

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

                settings.printMediumType(SRSPrintMediumType::File);

                settings.fileName(fileName);

                settings.fileFormat(SRSReportFileFormat::PDF);


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

                controller.parmReportContract().parmReportExecutionInfo(executionInfo);


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

                srsReportRunService.preRunReport(controller.parmreportcontract());

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

                parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

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


                reportBytes         = srsproxy.renderReportToByteArray(controller.parmreportcontract().parmreportpath(),parameterValueArray,settings.fileFormat(),settings.deviceinfo());


                if (reportBytes)

                {

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

                    str fileContentType = System.Web.MimeMapping::GetMimeMapping(fileName);

                    #define.dotpdf('.pdf')

                    #define.Underscore('_')

                    //StorageCredentials storageCredentials = new StorageCredentials("d365prodattachments", "+ZD2KZ/TtqjzDu/Lrvotjus3KZAhDnJqluEIpDzwcagkMY5Y3IR77M8u6pYoN9f/8bHJA1tg4mE4+AStevsK/Q==");

                    StorageCredentials storageCredentials = new StorageCredentials(InventParametersLocal.LSBlobStorageName, InventParametersLocal.LSBlobStorageConnectionString);

                    CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    //CloudBlobContainer cont = blobClient.GetContainerReference("d365/FR40/MovementJournal");

                    //str path = 

                    CloudBlobContainer cont = blobClient.GetContainerReference(InventParametersLocal.LSBlobStoragePath + '/' +inventJournalTableLocal.JournalId);

                    CloudBlockBlob cloudBlockBlob = cont.GetBlockBlobReference(fileName);

                    cloudBlockBlob.UploadFromStream(stream, null, null, null);

                }

            }

        }


Thank you. This code of Dynamicscommunity101 is related to generate SSRS report and upload to blob storage