using Renci.SshNet; using Renci.SshNet.SftpClient; using Renci.SshNet.Common; using Renci.SshNet.Sftp; using Renci.SshNet.Sftp.SftpFile; using System.IO; class UploadSSRSReportToSFTPService_Custom extends SysOperationServiceBase { public void processOperation(UploadSSRSReportToSFTPContract_Custom _contract) { Query orderQuery; QueryRun queryRun; date processedDate; orderQuery = _contract.getQuery(); queryRun = new QueryRun(orderQuery); processedDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()); while (queryRun.Next()) { custInvoiceJour custInvoiceJour = queryRun.get(tableNum(custInvoiceJour)); this.sendFileToSFTP(custInvoiceJour); } } public void sendFileToSFTP(custInvoiceJour _custInvoiceJour) { str ext = SRSPrintDestinationSettings::findFileNameType(SRSReportFileFormat::PDF, SRSImageFileFormat::BMP); Filename filename = _custInvoiceJour.InvoiceId + ext; System.Byte[] reportBytes = this.renderReportToBinaryArray(_custInvoiceJour, filename); if (reportBytes) { using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes)) { try { str success = 'fail'; CustParameters parameter = CustParameters::find(); str host, username, password, destinationPath; int port; host = parameter.SFTPHostName_Custom; port = str2Int(parameter.SFTPPort_Custom); username = parameter.SFTPUserName_Custom; password = parameter.SFTPPassword_Custom; destinationPath = parameter.SFTPDestinationPath_Custom; SftpClient sftpClient = new SftpClient(host, port, username, password); sftpClient.OperationTimeout = System.TimeSpan::FromMilliseconds(60000); sftpClient.connect(); sftpClient.ChangeDirectory(destinationPath); if (sftpClient.IsConnected) { sftpClient.UploadFile(memoryStream , destinationPath + "@SYS35673" + fileName , null); success = 'pass'; } sftpClient.Disconnect(); sftpClient.Dispose(); if(success == 'pass') { Info(strFmt('@@Custom:UploadReportToSFTPSuccess', filename)); } else { Error('@@Custom:UploadReportToSFTPError'); } } catch (Exception::CLRError) { System.Exception ex = ClrInterop::getLastException(); if (ex != null) { ex = ex.get_InnerException(); if (ex != null) { error(ex.ToString()); } } } catch (Exception::Error) { Error("@@Custom:UploadReportToSFTPError"); } } } } /// <summary> /// Render report to byte array /// </summary> /// <param name = "_contract">contract class object</param> /// <param name = "_documentType">document type</param> /// <param name = "_filename">filename for the report</param> /// <returns>report as a byte array</returns> // private System.Byte[] renderReportToBinaryArray(Object _contract, PrintMgmtDocumentType _documentType, filename _filename) private System.Byte[] renderReportToBinaryArray(custInvoiceJour _custInvoiceJour, Filename _fileName) { SalesInvoiceContract rdpContract = new salesInvoiceContract(); rdpContract.parmRecordId(_custInvoiceJour.RecId); PrintMgmtReportFormatName printMgmtReportFormatName = PrintMgmtDocType::construct(PrintMgmtDocumentType::SalesOrderInvoice).getDefaultReportFormat(); SrsReportRunController srsReportRunController = new SrsReportRunController(); srsReportRunController.parmReportName(printMgmtReportFormatName); srsReportRunController.parmExecutionMode(SysOperationExecutionMode::Synchronous); srsReportRunController.parmShowDialog(false); srsReportRunController.parmReportContract().parmRdpContract(rdpContract); 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(_fileName); 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()); System.Byte[] reportBytes = srsproxy.renderReportToByteArray(srsReportRunController.parmreportcontract().parmreportpath(), parameterValueArray, printerSettings.fileFormat(), printerSettings.deviceinfo()); return reportBytes; } }
This blog is contains coding reference related to Microsoft AX 2012 and D365 finance and operations and Power platform
Tuesday, January 16, 2024
Upload SSRS report to SFTP using Renci.SShnet DLL in path /Folder/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment