Send SSRS Report as Email
7. January 2013 5 Comments
The SSRS Report Viewer in AX 2012 does not have a built in Email function. However, there are some implementations available to add an email button in SharePoint or ASP.NET pages, e.g. Codeproject . The idea is simple, save the report as PDF, open outlook email and add the PDF file as attachment. Follow these steps to implement an Email button for Dynamics AX.
In the AOT add a reference to the Microsoft.Reporting.WinForms DLL. It contains the SRS Report Viewer class. The DLL can be found here: C:\Program Files (x86)\Microsoft Visual Studio 10.0\ReportViewer. Make sure you are using the corresponding assembly version as your SQL Server.
Open the SrsReportViewer Form in the AOT, and add a button to the ButtonGroup. Call it “SendReport”.
Add the following Code to the clicked method:
void clicked()
{
Microsoft.Dynamics.AX.Frameworks.Controls.ReportViewer.AxReportViewer axviewer;
Microsoft.Reporting.WinForms.ReportViewer nviewer;
Microsoft.Reporting.WinForms.LocalReport report;
System.Exception ex;
System.Array bytear;
System.Object no;
System.IO.File file;
str tmpPath;
System.IO.FileStream fs;
SmmOutlookEmail smmOutlookEmail = new SmmOutlookEmail();
super();axviewer = AxReportViewer.control();
nviewer = axviewer.get_ReportViewerControl();
try
{
//render as PDF
report = nviewer.get_ServerReport();
bytear = report.Render("PDF");
no = bytear;//path to temp. files
tmpPath = System.IO.Path::GetTempPath();
tmpPath = tmpPath + "report.pdf";
//save to file
fs = System.IO.File::Create(tmpPath);
fs.Write(no,0,bytear.get_Length());
fs.Flush();
fs.Close();//open outlook email
if (smmOutlookEmail.createMailItem())
{
smmOutlookEmail.isHTML(true);
smmOutlookEmail.addFileAsAttachment(tmpPath);
smmOutlookEmail.sendEMail(smmSaveCopyofEmail::No);
}}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
}
}
Save, Compile and open a report. Enlarge the “Options” and you’ll see the new Email button.
By clicking the Email button, the report is rendered as PDF, stored locally in your temp. files and attached to a new outlook email.
This is a very simple implementation. You may add more customizations, e.g. take the report parameters into account to initialize the TO and CC Email fields.
Hi Markus, thanks for your post. I have a question, do you know how hide botton “save” in the ssrsreportviewer for only one report?
Hi Markus,
Thanks for the post. I think SRSReportRunController can also be directly used to send emails. Look at this post from the MS BI team http://blogs.msdn.com/b/dynamicsaxbi/archive/2012/03/09/how-to-directing-reports-to-email.aspx
thanks for your reply, I’ll check the link
Hello Markus,
Very helpful post indeed! I just like to add that at least in my case the Reference DLL’s name was Microsoft.ReportViewer.WebForms, thus changing code to the following:
Microsoft.Reporting.WebForms.ReportViewer nviewer;
Microsoft.Reporting.WebForms.LocalReport report;
Kind regards,
Donatas
Pingback: Update: Send SSRS Report as Email (with meaningful PDF File name) | ErpCoder