Modify or Add Values to Forms in EventHub Events - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Modify or Add Values to Forms in EventHub Events

  • Modify or Add Values to Forms in EventHub Events
  • 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?

  • 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)
                     
                
     
            

  • 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!

  • 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.

  • 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.

  • 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.

  • the same problem. any sollution?
  • 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?
  • 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.
  • Can You help me please? how to get the saved FormEntry  id in the FormSaved event?

    I see anly the formid :(