Custom Actions
Add user defined buttons to the toolbar and invoke custom actions using the Report Viewer property. You can create a custom email button with the rendered report to users.
Add email button
-
Create an email button in the toolbar using the
CustomItemsproperty with the values such asGroupIndex,Index,Type,CssClass, andTooltip. Thetool-bar-item-clickevent triggers when you click the email button. -
Access the Report Viewer model and create a JSON array for sending requests to the Web API Server.
-
You can use the following codes to add email button from controller and passing the data to view using
ViewBag.public ActionResult Index() { ToolbarSettings toolbarSettings = new ToolbarSettings(); toolbarSettings.CustomItems = new List<CustomItem>(); var customItem = new CustomItem() { GroupIndex = 1, Index = 1, CssClass = "e-icon e-mail e-reportviewer-icon", Type = BoldReports.ReportViewerEnums.ToolBarItemType.Default, Id = "E-Mail", Tooltip = new ToolTip() { Header = "E-Mail", Content = "Send rendered report as mail attachment" } }; toolbarSettings.CustomItems.Add(customItem); ViewBag.toolbarSettings = toolbarSettings; return View(); } -
You can use the following codes to set an
toolbar-settingsproperty at client side.<bold-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" toolbar-settings="ViewBag.toolbarSettings"> </bold-report-viewer> -
You can use the following codes to create an
tool-bar-item-clickevent at client side.<bold-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" tool-bar-item-click="onToolBarItemClick"> </bold-report-viewer> <script type="text/javascript"> function onToolBarItemClick(args) { alert('Action Triggered'); } </script> -
You can use the following codes to get
toolbar-settingsproperties on a dynamic object usingViewBag.toolbarSettingsand invoke custom actions.<bold-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" toolbar-settings="ViewBag.toolbarSettings" tool-bar-item-click="onToolBarItemClick"> </bold-report-viewer> <script type="text/javascript"> function onToolBarItemClick(args) { if (args.value == "E-Mail") { alert('Action Triggered'); } } </script>
Create custom email action
-
Create a new action method
SendEmailin the Web API service. -
Export the report to the required type using the
ReportHelper.GetReportmethod to send a report stream as an attachment. -
The following code sample exports the report to stream and send it as an attachment to a specified mail address. In the code, the
SmtpClientmethod is used to send the report as an email attachment.public object SendEmail([FromBody] Dictionary<string, object> jsonResult) { string _token = jsonResult["reportViewerToken"].ToString(); var stream = ReportHelper.GetReport(_token, jsonResult["exportType"].ToString(), this, _cache); stream.Position = 0; if (!ComposeEmail(stream, jsonResult["reportName"].ToString())) { return "Mail not sent !!!"; } return "Mail Sent !!!"; } public bool ComposeEmail(Stream stream, string reportName) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.IsBodyHtml = true; mail.From = new MailAddress("[email protected]"); mail.To.Add("[email protected]"); mail.Subject = "Report Name : " + reportName; stream.Position = 0; if (stream != null) { ContentType ct = new ContentType(); ct.Name = reportName + DateTime.Now.ToString() + ".pdf"; System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(stream, ct); mail.Attachments.Add(attachment); } SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "xx"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); return true; } catch (Exception ex) { throw ex; } return false; }In the above code sample, the report is exported to PDF format and send to users using the
SmptClientmethod.