Running a SSRS report via code
Sometimes we would like to run an SSRS report via code to produce a report without user interaction or to call it during a process. To do this we could call the menu item that launches the report, but more often than not we would like to prepopulate it or call it without user interaction. To call the class based report we need the following items:
- The report’s controller class or if it does not have one SrsReportRunController: The controller class will control the execution of the report. It also contains the query that will be used to run the report if you need to modify it, you can access it via the getFirstQuery method.
- The report’s contract class (if it has one): The contract class will have all of the parameters used to run the report. Please note: these could be hidden to the end user so please make sure you examine the class and the SSRS report design.
- SRSPrintDestinationSettings: this will store the output location, which printer etc.
Below is a job running the System user license count report to screen (it works in all companies and only requires one parameter).
static
void runReportViaCode(Args _args)
{
SrsReportRunController controller = new SrsReportRunController();
SysUserLicenseCountRDPContract contract = new SysUserLicenseCountRDPContract();
SRSPrintDestinationSettings printSettings;
controller.parmReportName(ssrsReportStr(SysUserLicenseCountReport, Report)); // Define report and report design to use
controller.parmShowDialog(false); // Use this to hide the report dialog
contract.parmReportStateDate(systemDateGet()); // Set all of the desired paramter values
controller.parmReportContract().parmRdpContract(contract); //Pass the contract class in to the controller
// Get and set the print settings as needed
printSettings = controller.parmReportContract().parmPrintSettings();
printSettings.printMediumType(SRSPrintMediumType::Screen);
controller.startOperation(); //Run the report
}