2

We have data stored in Custom database and we want to display the table's data in WFFM form field.

We are looking for a generic solution so that we can plug this into any website (multisite and multilingual website).

As Sitecore WFFM provides personalization with rules and action, here we see the possibility to get the solution with some customization.

  • Currently, we have one table in database and form is already designed, We want to show the details when the user logs into the website . – Abhimanyu Singh Nov 27 '17 at 8:01
  • If it is simple values, you can pass as queryString parameters into form page. WFFM will populate its fields with those data. sitecorefootsteps.blogspot.com/2015/04/… – scFootsteps Nov 27 '17 at 8:10
  • Our intention is to populate the field with the data which is stored in the database. – Abhimanyu Singh Nov 27 '17 at 8:17
  • 2
    You really should clarify your question and provide relevant details of what you want to achieve instead of leaving it to people to guess. Be specific and provide examples. – jammykam Nov 27 '17 at 23:01
3

I'm making a number of assumptions here (such as Sitecore 8.x+ and also WFFM MVC and not web forms and that this is to create a field of type dropdown). However If I'm right on these assumptions you probably need to do something like the following which inherits from the default dropdownlistfield and adds custom values from an external source:

The wffm field:

/// <summary>
/// used by web forms only and for settings in wffm designer
/// </summary>

public class ExternalDropDownList : DropList
{

    public new ListItemCollection SelectedValue
    {
        get { return this.selectedItems; }
        set { this.selectedItems = value; }
    }

    protected override void InitItems(ListItemCollection items)
    {
        base.InitItems(items);
    }

    protected override void DoRender(HtmlTextWriter writer)
    {
        base.DoRender(writer);
    }

}

The rendered field:

  // <summary>
    /// used to render the form field view (Views/Form/EditorTemplates/DropDownListField.cshtml)
    /// </summary>
public class ExternalDropDownListField : DropListField
{

    private string InitialSelection { get; set; }

    public override void Initialize()
    {
        Items = new List<SelectListItem>();

        var externaldbItems = dbFactory.GetItems(); //your db access here

        if (Parameters.ContainsKey("initialselection"))
            InitialSelection = Parameters["initialselection"];

        var externalItemSelected = false;
        foreach (var item in externaldbItems)
        {
            if (string.Equals(item.Field1, InitialSelection, StringComparison.InvariantCultureIgnoreCase))
                externalItemSelected = true;

            Items.Add(new SelectListItem
            {
                Value = item.Field1,
                Text = item.Field2,
                Selected = externalItemSelected
            });
        }

        base.Initialize();
    }

Lastly you need to add this into Sitecore as a custom field here: /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types

Like so:

enter image description here

Obviously you will need to add in access to your db via some kind of factory or ORM on this line:

var externaldbItems = dbFactory.GetItems();

  • Thanks for the suggestion . Yes, is one of the ways to pull the value from the database . Does it require every filed type to be a custom filed? – Abhimanyu Singh Nov 27 '17 at 13:52
  • How many fields from the database do you want to show? If there are multiple then you could extend the configuration of the field in the WFFM designer to include options on the field name, this could then be set when the field is added to the form. Then where you retrieve the value/values you could use this field name to get the right one. This would avoid building a custom field for every field type. It's hard to say without knowing what your db data looks like though. – Adam Seabridge Nov 27 '17 at 14:10
  • For initial display, you may consider we are showing a user profile which includes all profile data like title, name, address etc. – Abhimanyu Singh Nov 27 '17 at 14:18
  • Ok well I don't really know what other data you are collection on your form but what I've said above regarding a configurable field should work for you. You could then create one instance of this for each field type (textbox / dropdown list / etc) and then have a configuration value to set which user profile field it is when the field is added to the form. – Adam Seabridge Nov 27 '17 at 14:30
  • Thanks for suggesting good approach, we are taking it positively and also working on an alternate approach.. – Abhimanyu Singh Nov 27 '17 at 16:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.