3

I built a custom form with Sitecore MVC, which has an email field section, however, if a user is logged in, i want the user email to be auto populated in the email section.

How can i get the email of the currently logged in user in Sitecore?

1 Answer 1

4

So it depends where you are storing the email.

Using xDB

If its in the xDB facet, what key did you store the email under. If you stored it under Work Email, you would retrieve it like this.

if (Sitecore.Analytics.Tracker.Current.Contact != null)
{
    var emailFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");
    //Check if an work email address already exists for the contact
    if (emailFacet.Entries.Contains("Work Email"))
    {
        IEmailAddress email = emailFacet["Work Email"];
        return email.SmtpAddress;
    }
}

Using user's account

If you identified the user by their email address.

var email = Sitecore.Context.User.LocalName

Using user's profile

If you put the email on the user's profile when you created the user.

var email = Sitecore.Context.User.Profile.Email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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