Catch Custom Form Widget Submit Event
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.
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?
Hi
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
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);
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;
Where is FormSavedEvent defined? I paste in the code but it is not defined. Using sf6.3.5000
Found what appears to work. At least in 6.3.5000
void Bootstrapper_Initialized(object sender, ExecutedEventArgs e)
ObjectFactory.Container.RegisterType<PageEditorRouteHandler, CustomPageEditorRouteHandler>();
if (e.CommandName == "RegisterRoutes")
EventHub.Subscribe<Telerik.Sitefinity.Modules.Forms.Events.IFormEntryCreatedEvent>(evt => FormEventHandler(evt));
protected virtual void FormEventHandler(IFormEntryCreatedEvent 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);