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?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communitySo it depends where you are storing the email.
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;
}
}
If you identified the user by their email address.
var email = Sitecore.Context.User.LocalName
If you put the email on the user's profile when you created the user.
var email = Sitecore.Context.User.Profile.Email