+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Catch Custom Form Widget Submit Event

Catch Custom Form Widget Submit Event

3 posts, 0 answered
  1. Richard
    Richard avatar
    103 posts
    Registered:
    04 Apr 2013
    04 Jun 2015
    Link to this post

    I've created a custom form widget. I'd like to be able to capture the 'submit' event for the following scenario. A user types in their email address and checks a checkbox that indicates they want to receive a copy of the form submission. By default, the form can notify a user when there is a submission, but we'd like to create a 'comfort' option so the user who fills out the form also receives the email. 

    Is it the best method to intercept a successful form submission in the custom widget? Or am I thinking in the wrong location. 

  2. Richard
    Richard avatar
    103 posts
    Registered:
    04 Apr 2013
    04 Jun 2015 in reply to Richard
    Link to this post

    Likely I will be able to do this with the initialize event override, and perform the action on a postback.... but.... the next question then would be,...

     is there a method to add an email address on the fly to the form send? so lets say, admin@blah.com receives a copy of all form submissions. But if user@somewhere.com fills out the form and indicates they want to receive a copy, is there a way to add user@somewhere.com to the recipients of the form submission? Only for that submit?

    Or, then the other way would be for the widget to get all the field values in the form, and just send to that email address on the widgets postback.... that way could be cumbersome since the method would need to then also be dynamic in the sense that if the form is modified anytime, the fields reflected automatically to the custom widgets email.... 

    Ideas?

  3. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    09 Jun 2015
    Link to this post
    Hi Richard,

    You can use the FormSavedEvent to catch a form submission. You can do it the following way, by using the form events from the EventHub:
    http://docs.sitefinity.com/for-developers-ajax-forms-events

    Copy Code
    protected void Application_Start(object sender, EventArgs e)
           {
               Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(this.Bootstrapper_Initialized);         
           }
     
           private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
           {
               if (e.CommandName == "RegisterRoutes")
               {
                   EventHub.Subscribe<FormSavedEvent>(evt => FormEventHandler(evt));
               }
           }
     
           protected virtual void FormEventHandler(FormSavedEvent evt)
           {
               var formTitle = evt.FormTitle;
               var controls = evt.Controls;
     
               Dictionary<string, string> values = new Dictionary<string, string>();
               foreach (var control in controls)
               {
                   var value = control.Value != null ? control.Value.ToString() : null;
                   var fieldName = control.FieldName;
                   values.Add(fieldName, value);
               }
           }

    You can also get the submitted form controls values.

    You can get the form subscribed emails from the backend form settings the following way:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Telerik.Sitefinity.Modules.Forms;
    using Telerik.Sitefinity.Services;
    using Telerik.Sitefinity.Services.Notifications;
     
    namespace SitefinityWebApp
    {
        public partial class GetFormSubscribersMailsForm : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               var emails = this.GetFormSubscribersMails("MyForm");
            }
     
            public List<string> GetFormSubscribersMails(string formTitle)
            {
                var formsManager = FormsManager.GetManager();
                var form = formsManager.GetForms().Where(f=> f.Title == formTitle).FirstOrDefault();
                var notificationService = SystemManager.GetNotificationService();
                var serviceContext = new ServiceContext("ThisApplicationKey", FormsModule.ModuleName);
     
                var subscribedEmails = notificationService.GetSubscribers(serviceContext, form.SubscriptionListId, null)
                    .Where(s => s.Email == s.ResolveKey)
                    .Select(s => s.Email)
                    .ToList();
     
                return subscribedEmails;
            }
        }
    }

    This will get the exact emails entered through the backend UI form settings.
    Note that this will return only the emails entered in the settings tab textbox. If you subscribe through the form's Actions menu, for instance, this email will not be retrieved.


    This shows how the subscriber mails are associated with the form.
    You should either modify dynamically the subscribers list and subscribe another email using the notifications service and then unsubscribe it, or implement custom additional logic to send an email.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
3 posts, 0 answered