6

I have a function to fill in a user's first name, last name and email address when they are logged into our web site. But, now that we have translated our pages into dozens of languages, a bug has come up. When the field name is translated from English, it is not found and the field is not populated. Other than adding each of the foreign languages to the conditional statement, is there a way to fix this bug by using the item name instead of the title?

private void FillInUserData(Control control)
{
    foreach (Control child in control.Controls)
    {
        if (child is BaseControl)
        {
            if (child is InputControl)
            {
                InputControl field = (InputControl)child;

                if (field.Title.Equals("Email Address"))
                {
                    field.Text = UserSession.EmailAddress;
                }
            }
        }
    }
}

enter image description here

2

If you are trying to fill the field based on the current user, why not use the rules engine instead? There are default rules available, documented here.

On your field, edit the rule. You can select condition(s) based on users (from a certain domain, or whatever you need) and add as action the "Use the default value from user profile specific field". This reads the value of the specific parameter in the user profile of the current website visitor and makes it the current field value. As this rule is set on the field, you'll have no more trouble with translations.

What condition you should use, depends on the requirements. But the "Security" section in the rules is a good starting point. You can verify if the current user is anonymous (or not), whether he is in a domain or role, ... In your case that could be a not-anonymous user in thee extranet domain.

  • This looks pretty easy, but is it only available WFFM 8+ version? – Tamás Tárnok Dec 14 '16 at 9:26
  • I used it on a 8+, but according to the 2.4 guide on sdn it should be available there as well (so for Sitecore 7 versions). – Gatogordo Dec 14 '16 at 9:30
  • Could you tell me exactly what condition I am supposed to choose? – ADH Dec 14 '16 at 18:45
  • Added info to the answer - can only give an indication as I don't know your requirements nor security setup. – Gatogordo Dec 14 '16 at 19:17
  • I created a new question to figure out the rule: sitecore.stackexchange.com/questions/3402/… – ADH Dec 15 '16 at 18:29
1

I would create a new WFFM field in Sitecore and then you have the full control of this field and it's not hacky.

1. Backend implementation

Here is the implementation which renders the user email into a div element.

using System;
using Sitecore.Form.Core.Controls.Data;
using Sitecore.Form.Web.UI.Controls;
using Sitecore.Web.UI.HtmlControls;

namespace MyProject.WFFM.Fields
{
    public class UserDataEmailField : BaseControl
    {
        private Panel container = new Panel();

        public override ControlResult Result
        {
            get
            {
                var item = Sitecore.Context.Database.GetItem(this.FieldID);
                return new ControlResult(item.Name, Sitecore.Context.User.Profile.Email, string.Empty);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.Controls.AddAt(0, this.container);
        }
    }
}

2. WFFM field item in Sitecore

Then you need to create the new WFFM field item in Sitecore like the following.

enter image description here

After you are done with this you can use this new field on your forms.

1
private void FillInUserData(Control control)
{
    foreach (Control child in control.Controls)
    {
        if (child is BaseControl)
        {
            if (child is InputControl)
            {
                InputControl field = (InputControl)child;

                if (((BaseControl)child).ControlName.Equals("Email Address"))
                    field.Text = UserSession.EmailAddress;
            }
        }
    }
}    

I figured this syntax out by debugging:

enter image description here

  • For me sounds like not really safe, what happens if the author change the name of item name of that field? – Tamás Tárnok Dec 16 '16 at 11:53
  • Then it won't work. – ADH Dec 16 '16 at 12:49
  • I specifically asked if there was a way to change the code to use the item name. This bug is on Production and it needed to be fixed right away. I will implement a safer solution in the long term, but right now I just needed to use the item name. – ADH Dec 16 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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