Custom actions
This section explains you the steps required to add user defined buttons in Report Viewer toolbar and invoke custom actions.
Send a report as an email attachment
This topic describes steps required to create custom email option and share a report as an email attachment to other users.
Add email button in Report Viewer
-
Create an email button in the toolbar using the
customItemsproperty with the values such asgroupIndex,index,itemType,cssClass, andtooltip. ThetoolBarItemClickevent 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 create an event with custom action.
<script type="text/javascript"> $(function () { $("#viewer").boldReportViewer({ reportServiceUrl: "/api/ReportsWebApi", reportPath: '~/App_Data/Sales Order Detail.rdl', toolbarSettings: { showToolbar: true, items: ej.ReportViewer.ToolbarItems.All & ~ej.ReportViewer.ToolbarItems.Print, customItems: [{ groupIndex: 1, index: 1, type: 'Default', cssClass: "e-icon e-mail e-reportviewer-icon", id: 'E-Mail', tooltip: { header: 'E-Mail', content: 'Send rendered report as mail attachment' } }] }, toolBarItemClick: 'ontoolBarItemClick' }); }); //Toolbar click event handler function ontoolBarItemClick(args) { if (args.value == "E-Mail") { var proxy = $('#viewer').data('boldReportViewer'); var Report = proxy.model.reportPath; var lastsIndex = Report.lastIndexOf("/"); var reportName = Report.substring(lastsIndex + 1); var requrl = proxy.model.reportServiceUrl + '/SendEmail'; var _json = { exportType: "PDF", reportViewerToken: proxy._reportViewerToken, ReportName: reportName }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: requrl, data: JSON.stringify(_json), dataType: "json", crossDomain: true }) } } </script>
Create custom email action
To create custom email action, follow these steps:
- 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 SmtpClient method is used to send the report as an email attachment.
```csharp
public object SendEmail(Dictionary<string, object> jsonResult)
{
string _token = jsonResult["reportViewerToken"].ToString();
var stream = ReportHelper.GetReport(_token, jsonResult["exportType"].ToString());
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)
{
return ex.ToString();
}
return false;
}
```
> In the above code sample, the report is exported to PDF format and sent to users using the `SmptClient` method.- Build and run the application.