Skip to main content

Web Forms For Marketers - Custom Unique ID Field Type

I recently had a request from the business to implement a unique 8-digit (auto-incrementing) Reference number for one of their Forms. Doing a quick scan in the available Field types, I found that there wasn't any type available that fits my requirement. 
I had two options to consider:

  1. Override the Save to DB action: by overriding the Execute method and replacing the AdaptedResultList with my own which queries the WFM DB and looks for the Max Reference value and returns the incremented value at that instance; this worked well but I quickly ran into an issue wherein other Save Actions (like Send Email) were still using the original AdaptedResultList and thus the experience was inconsistent. If I were to go this route, I would have to override every possible Save action which is not very good. So I concentrated with my next option:  
  2. Create my own Custom Field Type: I created my own type, did the necessary changes in the WFFM module settings like (add a new Field Type referencing my new custom class). The key here is to override the ControlResult property of the SingleLineText field class so that instead of taking whatever was passed from the UI, it will instead get the value from the custom method, in my case to get the 8-digit Max Value from the Reference Field in the WFFM DB. See sample code below: You may notice that I used ADO and specifically looking for a Numeric 8-digit value starting with the number 1. e.g. 10000000.

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public class ReferenceNumberText : SingleLineText
        public override ControlResult Result
                return CreateNewControlResultWithValue(Title, GetMaxValue());
        private static ControlResult CreateNewControlResultWithValue(string name, string value)
            return new ControlResult(name, value,"");
        /// <summary>
        /// Note: FieldName should always have the term "Reference Number" appended
        /// </summary>
        /// <returns></returns>
        private static string GetMaxValue()
            var builder = new StringBuilder();
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["wfm"].ConnectionString))
                using (var command = connection.CreateCommand())
                    command.Connection = connection;
                        "SELECT MAX(CAST(Value AS INT)) FROM [Field] WHERE FieldName LIKE '%Reference Number' AND Value LIKE '1[0-9][0-9][0-9][0-9][0-9][0-9][0-9]' AND ISNUMERIC(Value) = 1");
                    command.CommandText = builder.ToString();
                    command.CommandType = CommandType.Text;
                    var value = command.ExecuteScalar();
                    return DBNull.Value == value ? "10000000" : ((int)value + 1).ToString(CultureInfo.InvariantCulture);


Popular posts from this blog

Add/Allow a different media URL prefix in Sitecore (aside from tilde ~) in Sitecore 7.0

We recently had a requirement when one of our external vendors required that our media URLs should not include the "~" in the link as their system could not process those correctly. I found a few articles in the web, but most of them would suggest changing the default behavior, i.e. any "new" media item would have the 'new' replacement character though still supporting the tilde "~". Based on the web articles, I started with the following config keys and section: Media.RequestExtension Media.MediaLinkPrefix customHandlers We did not want to change any of the default behaviors, we just needed a way to make Sitecore support the additional URL media prefix. While sifting through the configuration, I chanced upon the following section: <mediaPrefixes>  According to the comment above it: Allows you to configure additional media prefixes (in addition to the prefix defined by the Media.MediaLinkPrefix setting)            The

Add Export to File Functionality in Sitecore's Search Options

One of our business users was requesting for a listing of items they've already added and have it available in a format that can be opened in a spreadsheet (Excel). My initial thought was to create a blank aspx page and write all the logic to get the data in the code-behind, run it and save the resulting file to a csv then I'm done. But then it got me to think, it might be a better idea to have this functionality plugged in to Sitecore and made available for everyone to use.  Sitecore Bucket's Search Options fly-out seemed a good candidate for this feature (See image below). So doing some quick readings I got myself in to the "zone" and started implementing this quick and dirty PoC. Just to explain what it actually does. User will basically do a search, ( Note that all search options require some filters or search keyword before any of the options can be used ) clicks the Export to File, it pops up a dialog to confirm the action and executes, after w

Implementing a Secure Media library in Sitecore

Our internal clients wanted us to secure some of their resources stored in the media library so that only authenticated and authorized extranet users are able to view or download them. Our current setup has the following components, I'll simply focus on the 3rd and 4th points: Sharepoint Workflows on a Sharepoint 2007 Server. This serves as the working repository of the resource owners. Sitecore Sharepoint Integration Framework. This hooks up Sitecore with the designated document library in Sharepoint for all finalized files/documents Integration folder in the Media library Login page to handle the authentication Create your login page. You have the option to create your sublayout for this (mostly to control the page contents etc.) or you can just quickly create one and put it in one of your folders in your solution. I normally use CodeFile directive in my pages as we found it easier to maintain this type of setup in the long run. There are plenty of samples of the login