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

Forums / Developing with Sitefinity / Accessing submitted form data in code

Accessing submitted form data in code

5 posts, 0 answered
  1. Richard
    Richard avatar
    49 posts
    Registered:
    13 Jul 2010
    21 Aug 2012
    Link to this post
    Hello all.

    I have tried looking for this but I am obviously being a bit blind.

    I have a form, with a "Name used in code (for developers)" of sf_registration.

    How do I go about intercepting the sf_registration submisison? Ideally I'd like to get the data and send this through to our CRM instance but I just need to know how to hook my code this form.

    Any ideas or pointers to relevant docs/blogs?

    EDIT: I have tried following this here post but without success.

    All I want to do is get into the code!!! SO I have the following class

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Web;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Modules.Forms;
    using Telerik.Sitefinity.Modules.Forms.Web.UI;
    using Telerik.Sitefinity.Services;
    using Telerik.Sitefinity.Web.UI;
     
    namespace Valpak.Websites.Public51.Sitefinity
    {
        public class RegistrationForm : FormsControl
        {
            protected override void InitializeControls(GenericContainer container)
            {
                base.InitializeControls(container);
                this.BeforeFormAction += new EventHandler<System.ComponentModel.CancelEventArgs>(CustomFormsControl_BeforeFormAction);
            }
     
            private void CustomFormsControl_BeforeFormAction(object sender, CancelEventArgs e)
            {
                
                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();
                
            }
        }
    }

    And I have updated the settings accordingly (see forms control)

    However, when I stick breakpoints into the code, it doesn't get hit. Can someone point out the error in my ways?

    Thanks in advance.
  2. Richard
    Richard avatar
    49 posts
    Registered:
    13 Jul 2010
    21 Aug 2012
    Link to this post
    Right ok, the above does work but I have introduced this event handler which is hit when I save the form i.e. effectively click the submit button.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Web;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Modules.Forms;
    using Telerik.Sitefinity.Modules.Forms.Web.UI;
    using Telerik.Sitefinity.Services;
    using Telerik.Sitefinity.Web.UI;
     
    namespace Valpak.Websites.Public51.Sitefinity
    {
        public class RegistrationForm : FormsControl
        {
            protected override void InitializeControls(GenericContainer container)
            {
                base.InitializeControls(container);
                this.FormSaved += new EventHandler<System.EventArgs>(CustomFormsControl_FormSaved);
             
            }
     
            private void CustomFormsControl_FormSaved(object sender, EventArgs e)
            {
               //Do work here
            }
        }
    }

    This works and I can get to the goodies from here but is this 'correct'? or should I be hooking into the submit button event handler? If so, where's that then?!



  3. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    24 Aug 2012
    Link to this post
    Hi Richard,

    Your approach is OK as long as it does the job you need. What i'm puzzled about is that the previous implementation is not reached. Have you tried to download the sample project from Radoslav's blog post? It's at the bottom of the article. Also have you setup the notification settings for the forms responses? I have tested it on Sitefinity 5.1 3270 and it works fine. I just had to replace the Telerik assemblies with the new ones from my project's bin folder and add a reference to the sample project in my web application. From there I have overriden the CLR path of the built in FormsControl with the custom one. 

    It is worth mentioning how to actually access the different fields in the submitted response. Try using the following:

    //get the form fields
                var form = manager.GetFormByName("sf_myformname");
                var fields = (from ctrl in form.Controls.Where(c => c.Properties.Count(c2 => c2.Name.Equals("MetaField")) > 0).Cast<FormControl>()
                              select new ControlForDisplay
                              {
                                  fieldName = ctrl.Properties.FirstOrDefault(p => p.Name.Equals("MetaField")).ChildProperties.FirstOrDefault(cp => cp.Name == "FieldName").Value,
                                  fieldTitle = "TITLE",
                                  siblingId = ctrl.SiblingId,
                                  Id = ctrl.Id,
                                  controlType = "formField"
                              }).ToList<ControlForDisplay>();
     
                //get values of fields
                foreach (var item in fields)
                {
                    var title = item.fieldTitle;
                    var fieldValue = formResponse.GetValue(item.fieldName).ToString();
                    var controlType = item.controlType;
                }

    I hope this helps.
    All the best,
    Pavel Benov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  4. Erik
    Erik avatar
    1 posts
    Registered:
    24 Jul 2012
    08 May 2013 in reply to Pavel Benov
    Link to this post
    Hi I am just starting development on a Sitefinity 6.0.4100.0 project.  I have been working towards doing exactly this type of sitewide FormsControl event interception but it appears that I can no longer set the Control CLR Type or Virtual Path property via Administration -> Settings -> Advanced -> Toolboxes -> PageControls ->  Sections -> ContentToolboxSection -> Tools -> FormsControl.  If I change the ToolboxesConfig.config directly, I still don't hit breakpoints in my code.  Are these event hooks longer available in 6.0? or is there a different way of doing it?
  5. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    13 May 2013
    Link to this post
    Hi Erik,

    The method of registering custom controls in Sitefinity's toolboxes has not being changed, so if you modify the CLR type to your custom form control is should resolve properly and work. Make sure that your are registering the custom control by providing the CLR type like <Namespace>.<ClassName>.

    Additionally did you restarted the application after changing the ToolboxesConfig.config file by re-saving the web.config file? Try this also and let us know of the outcome.

    Regards,
    Pavel Benov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
5 posts, 0 answered