1

I'm trying to store an e-mail address submitted in a form to a new contact (first time) website visitor.

I'm using the below code from EmailAddressList section - Contact Facets, but the email address is not shown for the visitor in the Experience Profile. However, the Spam key and email address is shown for the visitor.

var preferredEmail = new EmailAddress("sample@sitecore.test", true);
var preferredKey = "Work"

var emailFacet = new EmailAddressList(preferredEmail, preferredKey)
{
    Others = new Dictionary<string, EmailAddress>()
    {
        { "Spam",  new EmailAddress("spam@me.com", false) }
    },
};

client.SetFacet<EmailAddressList>(new FacetReference(contact, EmailAddressList.DefaultFacetKey), emailFacet);

I tried multiple ways of the above code.. Nothing worked. What is wrong here? I'm using Sitecore 9.3

0

Please try below piece of code, Its working for me in Sitecore 9.3:

public void UpdateFacets(string email, Contact contact)
    {
        using (var client = SitecoreXConnectClientConfiguration.GetClient())
        {
            try
            {
                var changed = SetFacets(email, contact, client);
                if (!changed) return;
                client.Submit();                    
            }
            catch (XdbExecutionException ex)
            {
                Log.Error($"Could not update the xConnect Email facets", ex, this);
            }
        }
    }
    private bool SetFacets(string email, Contact contact, IXdbContext client)
    {
        if (string.IsNullOrEmpty(email))
        {
            return false;
        }
        var emails = contact.GetFacet<EmailAddressList>(EmailAddressList.DefaultFacetKey);
        if (emails == null)
        {
            emails = new EmailAddressList(new EmailAddress(email, false), "Preferred");
        }
        else
        {
            if (emails.PreferredEmail?.SmtpAddress == email)
            {
                return false;
            }
            emails.PreferredEmail = new EmailAddress(email, false);
        }
        client.SetFacet(contact, EmailAddressList.DefaultFacetKey, emails);
        return true;
    }

One more thing you need to take care, are you defining Email Facet key when you are fetching contact? If not please do that as below:

using (var client = _xdbContextFactory.CreateContext())
            {
                Contact contact = client.Get(contactReference, new ContactExpandOptions(EmailAddressList.DefaultFacetKey));
            }
| improve this answer | |
  • Thank you Himmat! Basically, I'm doing this for the first time visitor by creating a contact, so I can't use contact.GetFacet right? I have seen this code but I thought this would not be suitable for my scenario - first time visitor (where Contact.IsNew is true).. – Paulson Maclean Jul 29 at 16:18
  • I am doing the same thing. Getting visitor contact then identify that contact "Tracker.Current.Session.IdentifyAs(source, identifier);" and convert that Analytics contact to Xconnect contact. You can use contact.GetFacet. – Himmat Singh Dulawat Jul 30 at 5:07
  • Please check the code at github.com/AmitKumar-AK/CT.SC/blob/master/src/Feature/Accounts/… to set the email address – Amit Kumar Aug 2 at 20:38

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.