Modify or Add Values to Forms in EventHub Events

Posted by Community Admin on 04-Aug-2018 14:38

Modify or Add Values to Forms in EventHub Events

All Replies

Posted by Community Admin on 22-Jan-2015 00:00

Is it possible to modify or add values to  a Sitefinity form in any of the EventHub events (IFormValidatingEvent, FormSavingEvent, etc.)? It seems these events expose only "validating" controls or some other abstraction or interface rather than the underlying FieldControls themselves. This is true even of the IFormFieldValidatingEvent even though it claims to return the field being validated. The exposed controls have no setter and I see no path to the underlying FieldControl.  How can I change a form field value or add a hidden field value in these events?

Posted by Community Admin on 22-Jan-2015 00:00

It can be done! :)

Heres a sample where I add in some data after it's submitted:

/// <summary>
        /// Method to put the presenter data into a LGS submission
        /// </summary>
        /// <param name="entry"></param>
        public static void HandleLGS(IFormEntryCreatedEvent eventEntry)
            FormsManager formsManager = FormsManager.GetManager();
            string entryTypeName = String.Format("0.1", "Telerik.Sitefinity.DynamicTypes.Model", eventEntry.FormName);
            var entryType = formsManager.GetEntryType(entryTypeName);
 
 
            try
            
                var entry = formsManager.GetFormEntry(entryType.FullName, eventEntry.EntryId);
 
                var academicSessionId = entry.GetValue<string>("SessionID");
 
                if (academicSessionId != null)
                
                    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
                    Type academicSessionType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Sessions.AcademicSession");
 
                    // This is how we get the academicSession item by ID
                    Telerik.Sitefinity.DynamicModules.Model.DynamicContent academicSessionItem = dynamicModuleManager.GetDataItem(academicSessionType, new Guid(academicSessionId.Trim()));
 
                    var presenter = academicSessionItem.GetValue<string>("Presenter");
                    var sessionDate = academicSessionItem.GetValue<DateTime>("SessionDateTime");
                    var sessionTitle = academicSessionItem.GetValue<string>("Title");
 
 
 
                    // Get the form named "sf_testform"
 
                    bool edits = false;
 
                    if (String.IsNullOrEmpty(entry.GetValue<string>("SessionPresenter")))
                    
                        entry.SetValue("SessionPresenter", presenter);
                        edits = true;
                    
 
                    if (String.IsNullOrEmpty(entry.GetValue<string>("SessionName")))
                    
                        entry.SetValue("SessionName", sessionTitle);
                        edits = true;
                    
 
                    if (String.IsNullOrEmpty(entry.GetValue<string>("SessionDate")))
                    
                        entry.SetValue("SessionDate", sessionDate.ToString("MMM dd yyyy hh:mmtt"));
                        edits = true;
                    
 
                    if (String.IsNullOrEmpty(entry.Username))
                    
                        entry.UserId = ClaimsManager.GetCurrentUserId();
                        entry.Username = Util.GetUsername();
                        edits = true;
                    
 
                    if (edits)
                        formsManager.SaveChanges();
                
            catch(Exception ex)
                 
            
 
        

Posted by Community Admin on 22-Jan-2015 00:00

Steve,

Thanks for answering and the example.​

Does the IFormEntryCreatedEvent fire soon enough to change/add the values BEFORE the e-mail notifications, if any, are sent? I'm currently using the FormSavedEvent to add/modify after the fact, but it's too late in the lifecycle to affect the e-mails. Ideally, I'd like to alter these values before they are saved, but I'll take anything that precedes the e-mail notifications. I suppose I could override the notification logic for the forms in question, too, If necessary, but I'm trying to avoid messy and less maintainable "hacks."

 Thanks again!

Posted by Community Admin on 22-Jan-2015 00:00

I'm not 100% sure on that, maybe support could chime in?...but I would hazard a guess that you would maybe want to hook in before "Created"

 There's a FormSavingEvent that returns you a list of the Controls to perhaps modify.

Posted by Community Admin on 22-Jan-2015 00:00

I'm looking at the lifecycle for IFormCreatedEvent now to see if it's soon enough.  The controls returned by the IFormValidatingEvent, IFormFieldValidatingEvent  and FormSavingEvent don't have a setter for the control's Value, so these events are useless for my purposes. Also, since they occur before the form is created or saved, there's no way to use forms manager to retrieve an Id to make the updates using them. At least I can't find one.

​Thanks again.

Posted by Community Admin on 14-Sep-2016 00:00

the same problem. any sollution?

Posted by Community Admin on 14-Sep-2016 00:00

The only "solution" I found is, IMO, a hack. Trap the FormSaved event, use the GUID of the FormEntry to retrieve it using FormsManager and make and save/commit your changes.

The problem is that this fires the FormSaved event again, so you'll need logic to prevent duplicate actions--like sending emails--if you have them.

Posted by Community Admin on 14-Sep-2016 00:00

I want to change/add the values BEFORE the e-mail notifications.

I want catch the submited data to create the entity in the MS CRM, get the html link from CRM and add this link into the email notification.

how to add something into the form email notification before sending?

Posted by Community Admin on 14-Sep-2016 00:00

So did I, but I was unable to find a way to do it. I think it's impossible considering how the EventsHub and its objects are designed. Without a Setter in those objects, you can't do anything meaningful.

Posted by Community Admin on 17-Nov-2016 00:00

Can You help me please? how to get the saved FormEntry  id in the FormSaved event?

I see anly the formid :(

This thread is closed