The following blog post will examine how to implement notifications for Forms responses. We are going to implement this feature to be available out of the box, and you can use this quick and easy approach to have the notifications until we release them as part of the system. The built in Forms provides a number of events on which you can hook to in order to get custom validation, or custom actions when the form is submitted. We are going to use this extensibility point to implement our functionality.
Implementing the custom Forms control
First we need to inherit from the built-in FormsControl class and we need to wire the BeforeFormAction event handler. In this handler we are going to get the title of the form being submitted, the response, and construct the e-mail message. To be able to send the message we need to have valid SMTP settings in your System Configuration. Here is the code for the custom control:
namespace
Telerik.Sitefinity.Samples.Forms
{
public
class
CustomFormsControl: FormsControl
{
protected
override
void
InitializeControls(Web.UI.GenericContainer container)
{
base
.InitializeControls(container);
this
.BeforeFormAction +=
new
EventHandler<System.ComponentModel.CancelEventArgs>(CustomFormsControl_BeforeFormAction);
}
private
void
CustomFormsControl_BeforeFormAction(
object
sender, CancelEventArgs e)
{
//get the SMTP settings
var smtpSettings = Config.Get<SystemConfig>().SmtpSettings;
FormsManager manager = FormsManager.GetManager();
//get the form response by using the current form and the referral code
var formResponse = manager.GetFormEntries(FormData).Where(fE => fE.ReferralCode == FormData.FormEntriesSeed.ToString()).SingleOrDefault();
//construct mail message
MailMessage message =
new
MailMessage();
message.From =
new
MailAddress(smtpSettings.DefaultSenderEmailAddress);
message.To.Add(
new
MailAddress(
"yourMailAddres@domain.com"
));
StringBuilder sb =
new
StringBuilder();
sb.AppendFormat(
"You have one new form response on {0} form. The response is sent from {1} IP address"
, FormData.Title, formResponse.IpAddress);
message.Subject=
"Forms notification"
;
message.Body = sb.ToString();
message.IsBodyHtml =
true
;
message.BodyEncoding = Encoding.Unicode;
message.SubjectEncoding = Encoding.Unicode;
//send the notification
EmailSender.Get().Send(message);
}
}
}
For further information on how you can work with entries, please check out the Forms Module developer topics: Working with forms.
Registering the custom Forms control
After the control is implemented we have to substitute the built in one with the custom one. To do this, go to Administration -> Settings -> Advanced -> Toolboxes -> PageControls -> Sections -> ContentToolboxSection -> Tools -> FormsControl. There edit the Control CLR Type or Virtual Path property. Substitute the original value with the CLR Type of the custom control. Now whenever a user adds the Forms control to pages the custom one will be used instead of the built-in one.
Download sample project
You can download the project for the custom control from this location: Forms notifications sample project. Make sure that you resolve the references to Sitefinity and OpenAccess assemblies.