+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

How to implement notifications for incoming Forms responses

by Radoslav Georgiev

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.

9 comments

Leave a comment
  1. OC Jan 11, 2012
    How can you access the actual form response data? I want a summary of the response in the notification email.
  2. Radoslav Georgiev Jan 18, 2012
    Hi OC,

    Please refer to the above code sample, there you can see how to get an instance of the response which was just submited:
    //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();
  3. SR Jan 24, 2012
    If it is this simple why isn't supplied as part of the product by now - there's been enough service packs!
  4. OC Jan 24, 2012
    Radoslav,

    I see that I can get the Title and IP address and other metadata from this, but can't see how to access the actual data submittet in the form. I want to loop through the form feilds and print the values submitted.

    OC
  5. OC Jan 24, 2012
    Radoslav,

    I see that I can get the Title and IP address and other metadata from this, but can't see how to access the actual data submittet in the form. I want to loop through the form feilds and print the values submitted.

    OC
  6. Ivan Mar 09, 2012
    GetFormEntries returns FormEntry object from which you can extract the data you want. FormEntry implements IDynamicFieldsContainer, IContent. You can get all custom fields by using GetValue DataExtension method or  TypeDescriptor.GetProperties(object item);
  7. Dave Mar 21, 2012
    Without being able to include all of the user's responses I don't see much value in this custom forms control. Can someone please provide some sample code that loops through all of the user's responses and add's them to the email.

    Ivan, I think you know how to do it, can you please show us?
  8. ATP Mar 17, 2015
    How about sitefinity 7, is Notification automatic send when we use that code ?
  9. Rado Mar 17, 2015
    In version 7 you can subscribe to forms responses out of the box. No need for custom code

    Leave a comment