Custom Field Type For Sitecore Web Form For Marketers


I have seen many questions about adding custom field in WFFM and adding session or querystring values in WFFM form field on form load.

I also got a same request to add querystring value in form field on load. WFFM module doesn’t provide any out of the box functionality to add session or querysting value in form field.

So finally I decided to create Custom Field in WFFM and add querystring value in that field. I started digging into Sitecore.Forms.Custom.dll and found code for SingleLineText field.

I created a custom field item with name CustomSingleLineText under /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom folder inside Sitecore and created a class in my class library project.

I extended code of SingleLineText field class from Sitecore.Forms.Custom.dll and added logic to show Session or Querystring value in text field on form load, see below code.

using Sitecore.Form.Core.Attributes;
using Sitecore.Form.Core.Visual;
using Sitecore.Form.Web.UI.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo.Halper
{
     [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
     public class CustomSingleLineText : RegexInputControl
     {
         private static readonly string baseCssClassName = "scfSingleLineTextBorder";
        [VisualCategory("Validation"), VisualProperty("Maximum Length:", 2000), DefaultValue(256)]
        public int MaxLength
        {
            get
            {
               return this.textbox.MaxLength;
            }
            set
            {
               this.textbox.MaxLength = value;
            }
         }
         [VisualCategory("Validation"), VisualProperty("Minimum Length:", 1000), DefaultValue(0)]
         public int MinLength
         {
            get;
            set;
         }
         [VisualFieldType(typeof(CssClassField)), VisualProperty("Css Class:", 600), DefaultValue("scfSingleLineTextBorder")]
         public new string CssClass
         {
            get
            {
                return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
         }
         public CustomSingleLineText(HtmlTextWriterTag tag)
         : base(tag)
         {
             this.MaxLength = 256;
             this.MinLength = 0;
             this.CssClass = CustomSingleLineText.baseCssClassName;
          }
          public CustomSingleLineText()
          : this(HtmlTextWriterTag.Div)
          {
          }
          protected override void OnInit(EventArgs e)
          {
              this.textbox.CssClass = "scfSingleLineTextBox";
              this.help.CssClass = "scfSingleLineTextUsefulInfo";
              this.generalPanel.CssClass = "scfSingleLineGeneralPanel";
              this.title.CssClass = "scfSingleLineTextLabel";
              this.textbox.TextMode = TextBoxMode.SingleLine;
              this.Controls.AddAt(0, this.generalPanel);
              this.Controls.AddAt(0, this.title);
              this.generalPanel.Controls.AddAt(0, this.help);
              this.generalPanel.Controls.AddAt(0, this.textbox);
              // Please add your login for session or querysting value
              this.textbox.Text = "Add your session or querystring value";
          }
     }
}

After created class and added custom logic I have added values in Assembly and Class fields on CustomSingleLineText field item in Sitecore. To show Querysting value on form field I used custom field instead of default Single Line Text field.

Happy Coding 🙂

Advertisements
This entry was posted in Sitecore, Sitecore web form for marketers and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Custom Field Type For Sitecore Web Form For Marketers

  1. Pingback: Sitecore: Interview questions | Sitecore Tweaks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s