Creating a Custom Sitecore WFFM Field Rule Action

Sitecore's Web Forms for Marketers module (WFFM) ships with OOTB support for field-based rules. They include actions for setting default field values from a User Profile field, a visitor tag and a querystring, but what if we want to make our own that can retrieve a different value? Today, I will show you how to write a generic and flexible WFFM Field Action that will enable you to set a field's default value to the value of any property on the User Profile.


Recently, a colleague asked how he could autofill the username of a site member on a hidden field of a WFFM form. I first showed him where he could set rules on WFFM form fields in the Form Designer, and let him know that use a rule to preset the value of a WFFM field.

Rules Editor Button Location - Bottom of Left Pane in Form Designer

He came back to me with the following rule, with the text UserName set for the field name, and asked if I could help him figure out why it was not working.

Rule Built Using OOTB Action

I decompiled and took a look at the action that he was using. This is an OOTB WFFM rule action, and I was very disappointed to find that it only has support for setting the value to one of the following from the User Profile: 

  • Name
  • Full Name
  • Email
  • Comment

Unfortunately, none of the above are the username, so I decided to write a WFFM field action that will return the value of any specified, non-indexed (i.e. not an enumerable, array, etc.) property of the User Profile.


The code for this is actually pretty simple. We start off by confirming that the User and User.Profile are not null, and then we retrieve the value from the UserProfile by property name:

namespace MyNamespace.Sitecore.Forms.Core.Rules
    public class ReadPropertyFromUserProfile<T> : Sitecore.Forms.Core.Rules.ReadValue<T> where T : Sitecore.Forms.Core.Rules.ConditionalRuleContext
        protected override object GetValue() 
            var userAccount = (Account)Context.User;
            if (userAccount == null) 
                Log.Error($"'(Account)Context.User' was null while attempting to execute WFFM action '{this.GetType().Name}', typeof(this));
                return null;

            var profile = Context.User.Profile;
            if (profile == null) 
                Log.Error($"'Context.User.Profile' was null while attempting to execute WFFM action '{this.GetType().Name}', typeof(this));
                return null;

            // get the type of the profile
            var profileType = profile.GetType();
            // ]should be the property with the name specified by the Content Author in the Form Designer's field rules editor
            var propertyToRead = profileType.GetProperty(Name);
            if (propertyToRead == null) 
                Log.Error($"Property '{Name}' was not found on the {profileType.Name}" type while attempting to execute WFFM action '{this.GetType().Name}'", typeof(this));
            // return the value of the property
            return GetPropertyValue(propertyToRead, profile);

        protected virtual object GetPropertyValue(PropertyInfo property, UserProfile profile) 
            // this method can be overridden to add support for indexable properties
            return property.GetValue(profile);

Sitecore Configuration

In order to use the new rule action, you will need to create an Action definition item for it. Navigate to /sitecore/system/Settings/Rules/Definitions/Elements/Web Forms for Marketers Actions, add a new item and name it something like "Read Property From User Profile". 

Rule Action Definition Fields to Edit - Text and Type

Be sure to set the following two field values:

  • Text: use the default value from the [Name,,,specific] property on the user profile
  • Type: MyNamespace.Sitecore.Forms.Core.Rules.ReadPropertyFromUserProfile, MyNamespace.Sitecore.Forms.Core

That's it! You should be good to go with your new Web Forms for Marketers field rule action!

Open up the Form Designer, click on a field, scroll down to the bottom in the left rail and give your new rule action a whirl!

Rule Built Using New Action

Future Enhancements

There are many enhancements that we can make to this solution, and many of them are pretty simple. The following are some very low-effort enhancements that can be made in just a few minutes:

  • Support for indexable properties
  • Support for reading a property from a custom UserProfile
Post a Comment

  • Comment by knorock on Apr 23, 2021 - cialis online without prescription