Sunday, February 14, 2010

Interaction between the Report and the Hosting application, Email Rendering Extension and more

Preface
I was pretty busy during past week and did not get the good ideas for the new post. Therefore I am going to write about two fairly new features of Data Dynamics Reports. They are Action event of Windows Forms Viewer and WebViewer and Email Rendering Extension. They are not well-documented yet, therefore this post will be probably helpful for someone.

Action event
Data Dynamics Reports has the great interactivity capabilities. It supports the hyperlinks, drill-through links, drill-down links, interactive sorting, bookmarks and the document maps. However, sometimes developer needs a custom type of interactivity. For instance - the report shows the list of employees, if a user clicks on the employee name in the report, then e-mail of employee is added in the internal list in the hosting application. A user selects a bunch of employees, closes the report and clicks "Send" button in the hosting application. The e-mail with the subject "You are fired" is sent to each employee from the list. Some time ago, it was not possible to implement such scenario using Data Dynamics Reports. But the fabulous product team went ahead and implemented the new feature that is called "Action event". Windows Forms Report Viewer and Web Report Viewer now have Action event. It is raised when a user clicks on the hyperlink, bookmark link or drill-through link in the report that is displayed in the viewer. In the handler of Action event developer can perform the custom steps and cancel the further action processing. Here is the sample of code:

public partial class _Default : System.Web.UI.Page
{
   internal List<string> _emailsList = new List<string>();

   protected void Page_Load(object sender, EventArgs e)
   {
      WebReportViewer1.Action += WebReportViewer1_Action;
   }

   void WebReportViewer1_Action(object sender, ActionEventArgs e)
   {
      if(e.Action.ActionType == ActionType.HyperLink)
      {
          _emailsList.Add(e.Action.HyperLink);
         e.Cancel = true;
      }
       e.Cancel = false;
   }
}

ActionEventArgs instance that is passed in Action event handler contains all the needed information: the type of Action and the value of the hyperlink, drill-through link or bookmark link. The most amazing fact about Action event is the uniform way it is implemented. It works for Windows Forms Viewer, Web Report Viewer-HTML mode and Web Report Viewer-PDF mode. In all cases developer just needs to add the handler for Action event.

Email Rendering Extension
Email Rendering Extension is the new rendering engine that allows to generate the report output which is compatible with the most popular e-mail clients. The clients with which we tested Email Rendering Extensions are:

  • Outlook 2007

  • Outlook 2003

  • Gmail

  • Yahoo

  • Hotmail

  • Thunderbird


Email Rendering Extension supports two modes - it can write the output to the file or memory stream and it can build the instance of System.Net.Mail.MailMessage which is available via the pubic property. The MailMessage instance that is generated by Email Rendering Extension contains the html view of the message and all the attachments, i.e. images, if any. Here is the sample of the code which sends the report output to the list of recipients, it continues the code that is shown above:


public partial class _Default : System.Web.UI.Page
{
   internal List<string> _emailsList = new List<string>();

   protected void btnSend_Click(object sender, EventArgs e)
   {
      var re = new EmailRenderingExtension();
      var def = new ReportDefinition(new FileInfo(Server.MapPath("~/Email.rdlx")));
      var runtime = new ReportRuntime(def);
      runtime.Render(re, null);
      var emailMessage = re.MailMessage;
      emailMessage.Subject = "You are fired";
      foreach(string email in _emailsList)
         emailMessage.To.Add(email);
      var fromAddress = new MailAddress("boss@company.com","Big Boss");
      emailMessage.Sender = fromAddress;
      emailMessage.From = fromAddress;
      var smtp = new SmtpClient("127.0.0.1", 25);
      smtp.Send(emailMessage);
   }
}

In this code Email Rendering Extension does not render the report to the file or memory stream. MailMessage property is used instead.

No comments:

Post a Comment