By: Scott Gillis, Lead Consultant – With the addition of the Experience Database (xDB) and the Experience Profile (xProfile), we as Sitecore site owners now have more in-depth knowledge collected on visitors.
The full experience platform begins to hum and provides a targeted experience for visitors as they move from just a unique id floating through our site and become a known person. A visitor's move from just an id to a person with a 'face' is accomplished by performing an action on your site that gives some details about them. A normal way for this to occur is through the collection of information via a form, such as subscribing to an email list or requesting more information.
Once a user has become known to us, it would be nice to further engage them by cutting out a few steps in their next interaction with a WFFM form by pre-populating certain fields with information we already have collected.
My first inclination was, there must be a WFFM field level action that accomplishes this already, given all of the talk about a contact's details being in one place via the magic of xDB. So I opened up my 'Coffee for Life' contest form to see what is available, by selecting a field (1) and clicking the Edit button (2) under Rules.
In the Rule Set Editor, we see there are four actions specific to WFFM. I saw "use the default value from user profile specific field" action and thought "perfect". After selecting this action, you have to enter a specific field and it is just a text box.
I thought this was strange, so I opened up the rule in JetBrains' dotPeek (https://www.jetbrains.com/decompiler/). The specific assembly is Sitecore.Forms.Core.dll from there you are looking for Sitecore.Forms.Core.Rules.ReadValueFromUserProfile.
The rule ends up calling an extension method that reads three properties from the User Profile. The available values are 'Comment, Full Name, or Name'. IMPORTANT NOTE: The user profile is the data stored on a user who has an account and is managed via the User Manager, this is not xProfile data. There is no direct link between a User Profile and an xProfile. (It is common to tie the two via the xProfile's Identify Identifier field, but that is a topic for another article.)
Custom WFFM actions, as with other rules engine actions, require two components. First, there is the action code which is then wired to a Sitecore artifact that can be used in the Rules editor.
The code for an action inherits from Sitecore.Forms.Core.Rules.ReadValue and Sitecore.Forms.Core.Rules.ConditionalRuleContext. Per old, but very helpful, Sitecore documentation, the best approach is to decompile an existing action and copy the code and use that as a starting base. To save you some time, here is what a very streamlined action template looks like:
Step two of the process is to create a Sitecore artifact that defines your action and allows it to be selected.
With an Action item now created, you will need to populate a minimum of two fields, Text and Type. The Text field (1, found under the Data section) is the verbiage displayed to the editor in the Rules Editor dialog during rule and action selection. Therefore you want this written simple enough to be easily digested but also clear enough to indicate the intent of the rule/action.
Some important details about the contents of the text field as taken from the Sitecore CMS 6.1 Rules Engine Cookbook, page 7 [https://sdn.sitecore.net/upload/sitecore6/61/rules_engine_cookbook_sc61_a4.pdf].
Tokens in square brackets (--||) enable links allowing the user to specify parameters. The brackets contain four positional parameters separated by commas (--,||):
The second field to be concerned with is the Type field (2, found under the Script section). In this field, you will enter the class name with namespace and the assembly of your custom action.
With these fields populated and your custom code deployed, you can now begin using your action set to further customize WFFM.
Until part 2 is posted, you can check-out the full source solution on GitHub or, if you just want to use the macro and contact facet rule, download it from the Sitecore Marketplace. Until commenting gets implemented, feel free to tweet me questions or comments @thecodeattic or on Sitecore Slack Community as @gillissm.