In case we dont get personal facet, we need to set it first. Below code worked for us.
// Get contact from xConnect, update and save the facet
using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
{
try
{
var contact = client.Get<Sitecore.XConnect.Contact>(new IdentifiedContactReference(anyIdentifier.Source, anyIdentifier.Identifier), new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey));
if (contact != null)
{
if (contact.Personal() != null)
{
client.SetFacet<PersonalInformation>(contact, PersonalInformation.DefaultFacetKey, contact.Personal());
}
else
{
if (string.IsNullOrEmpty(args.PrincipalClaimsInformation.GivenName) && string.IsNullOrEmpty(args.PrincipalClaimsInformation.Surname) && string.IsNullOrEmpty(args.PrincipalClaimsInformation.DisplayName))
{
client.SetFacet<PersonalInformation>(contact, PersonalInformation.DefaultFacetKey, new PersonalInformation()
{
LastName = args.PrincipalClaimsInformation.DisplayName
});
}
else
{
client.SetFacet<PersonalInformation>(contact, PersonalInformation.DefaultFacetKey, new PersonalInformation()
{
FirstName = args.PrincipalClaimsInformation.GivenName,
LastName = args.PrincipalClaimsInformation.Surname
});
}
}
if (contact.Emails() == null)
{
var prefferedEmail = new EmailAddress(args.PrincipalClaimsInformation.Email, true);
var prefferedKey = "Work";
var emailFacet = new EmailAddressList(prefferedEmail, prefferedKey);
client.SetFacet<EmailAddressList>(new FacetReference(contact, EmailAddressList.DefaultFacetKey), new EmailAddressList(new EmailAddress("", true), ""));
}
client.Submit();
//SetPersonalInfo(session, args.PrincipalClaimsInformation);
//SetEmail(session, args.PrincipalClaimsInformation.Email);
// Remove contact data from shared session state - contact will be re-loaded
// during subsequent request with updated facets
manager.RemoveFromSession(Sitecore.Analytics.Tracker.Current.Contact.ContactId);
Sitecore.Analytics.Tracker.Current.Session.Contact = manager.LoadContact(Sitecore.Analytics.Tracker.Current.Contact.ContactId);
}
}
catch (XdbExecutionException ex)
{
// Manage conflicts / exceptions
}
}