Showing posts with label Export SSRS repot to sharepoint. Show all posts
Showing posts with label Export SSRS repot to sharepoint. Show all posts

Monday, January 23, 2023

Export SSRS report to SharePoint using X++

internal final class RunnableClass1
{   
    /// /// Runs the class with the specified arguments.
    /// The specified arguments.
    public static void main(Args _args)
    {
        DocuType docuType;
        select firstonly * from docuType
                    where docuType.PrintMgmtDocumentType == PrintMgmtDocumentType::SalesOrderInvoice;

        str URI = docuType.sharePointUrl();
        System.UriBuilder builder = new System.UriBuilder(URI);
        str extId = xUserInfo::getExternalId();
        Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider provider;
        Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation documentLocation = new Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation();       
        provider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(docuType.Host, docuType.Site, docuType.FolderPath, extId);
        System.Byte[] reportBytes = new System.Byte[0]();
        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
        reportBytes = RunnableClass1::renderReportToBinaryArray("100002");
     //   reportBytes = enc.GetBytes("YOUR STRING/TEXT");
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);
        //memoryStream = comma.getStream();
        documentLocation = provider.SaveFile(newGuid(), '100002.PDF', System.Web.MimeMapping::GetMimeMapping('100002.PDF'), memoryStream);
    }

    public static System.Byte[] renderReportToBinaryArray(SalesInvoiceId _salesInvoiceId)
    {
        str                     ret;
        CustInvoiceJour         custInvoiceJour;
        System.Byte[] reportBytes;
        select firstonly custInvoiceJour
            where custInvoiceJour.InvoiceId == _salesInvoiceId;

        if (custInvoiceJour)
        {
            str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP);
            PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat();
                                                                                                        
            SalesInvoiceContract salesInvoiceContract = new SalesInvoiceContract();
            salesInvoiceContract.parmRecordId(custInvoiceJour.RecId);

            SrsReportRunController  srsReportRunController = new SrsReportRunController();
            srsReportRunController.parmReportName(printMgmtReportFormatName);
            srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            srsReportRunController.parmShowDialog(false);
            srsReportRunController.parmReportContract().parmRdpContract(salesInvoiceContract);
            srsReportRunController.parmReportContract().parmReportExecutionInfo(new SRSReportExecutionInfo());
            srsReportRunController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());

            SRSPrintDestinationSettings printerSettings = srsReportRunController.parmReportContract().parmPrintSettings();
            printerSettings.printMediumType(SRSPrintMediumType::File);
            printerSettings.fileFormat(SRSReportFileFormat::PDF);
            printerSettings.parmFileName(custInvoiceJour.InvoiceId + ext);
            printerSettings.overwriteFile(true);
                                               
            SRSReportRunService srsReportRunService = new SrsReportRunService();
            srsReportRunService.getReportDataContract(srsReportRunController.parmReportContract().parmReportName());
            srsReportRunService.preRunReport(srsReportRunController.parmReportContract());
            Map reportParametersMap = srsReportRunService.createParamMapFromContract(srsReportRunController.parmReportContract());
            Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

            SRSProxy srsProxy = SRSProxy::constructWithConfiguration(srsReportRunController.parmReportContract().parmReportServerConfig());
                        
             reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(),
                                            parameterValueArray,
                                            printerSettings.fileFormat(),
                                            printerSettings.deviceinfo());

            //if (reportBytes)
            //{
            //    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes))
            //    {
            //        ret = System.Convert::ToBase64String(memoryStream.ToArray());
            //    }
            //}
        }

        return reportBytes;
    }

}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8