Using xProfile Data to Populate a WFFM - Part 2: Read from Contact Facet Action

Thursday, February 25, 2016 @ 03:50

By: Scott Gillis, Lead Consultant – Now that we know the basic steps for creating a custom WFFM action, let's work on solving our initial problem: How to use the knowledge we've gained about a visitor to simplify their engagement with us by pre-populating some of the form fields.

Sitecore Artifact

Setting up a Custom Action is explained in the first post of this series Introduction to WFFM Actions. Setup of our Custom Action for reading from the contact facet will look like the following:

Image One


Image Two


I want to call attention to the Text field and what we have entered into it. You will recall that the first value in the square brackets is the property in the code that the entered information will be sent to. We are using the default 'Name' property from the abstract ReadValue <T> class.

Next is three commas, which indicate no selection macro or starting point for the macro. Finally, the text that is shown to the user for them to click-on in the Rules Editor is entered. We have included some helper text that indicates the valid terms that can be used.

Action Code

  1. The always helpful using statement list
    • using Sitecore.Analytics;
    • using Sitecore.Analytics.Model.Entities;
    • using Sitecore.Diagnostics;
    • using Sitecore.Forms.Core.Rules;
    • using System.Linq;
  2. Next is the namespace and class details, which is nothing fancy and similar to our basic template

  3. Our custom action will be reading data from the currently tracked contact record. To be sure that the tracker is active and a contact is available, we will use the Sitecore Diagnostics Assert command. If the assert fails, this will provide a reasonable exception to be thrown and logged by Sitecore

  4. When selecting our action in the Rules Editor, we are required to enter text representing our chosen field from the contact facet. This text is then sent through a switch statement to retrieve the proper value for the current contact.

    As you will see in the code below, each use of GetFacet method requires both a typed interface as well as a string. These strings represent the different facets that have been tied to the contact record. They are defined in Sitecore.Analytics.Model.config

  5. If the entered text doesn't match or no contact data is found, a simple empty string is returned and used in the form for our field

Action as seen in the Rules Editor

Image Three

Clicking on the golden text will launch the text modal and allow the entry of a field name to be populated into the form.

Stay tuned for the next installment of the series that explains how to write a custom macro to allow for a pick-list of facet values.

Meanwhile, you can check-out the full source solution on GitHub. Until commenting gets implemented, feel free to tweet me questions or comments @thecodeattic or on Sitecore Slack Community as @gillissm.

Entire Series

Post One – Auto-Populate a WFFM Field
Post Two – Read from Contact Facet Action
Post Three – Rule Macro for Facet Selection
Post Four – Smart Form



Scott Gillis, Lead Consultant at Paragon and 2017 Sitecore MVP, has been working with Sitecore for several years. He has a deep passion for helping clients leverage their content and data into powerful new capabilities in Sitecore and has produced successful outcomes as the technical lead on numerous, complex implementations. Recently, Scott has been focusing on helping these clients take advantage of the wealth of data collected by Sitecore Experience Analytics.