0

I am working in Sitecore 9.3 instance. In XConnectFacets, I can see only 3 facets while debugging (see image), I have to updated PersonalInformation of the contact but it is not showing in XConnectFacets. Can anyone please suggest a possible resolution of this issue?

enter image description here

xConnectFacet.Facets["Personal"] returns null.
1

You must specify which facets should be returned with a contact or batch of contacts by passing an array of facet keys into the ContactExpandOptions. The only facets that are always returned if they exist are ConsentInfo and MergeInfo.

The method .GetFacet<T>() will return null if:

  • The facet was not requested as part of the ContactExpandOptions when the contact was initially retrieved
  • The facet has not been set for a particular contact
  • The combination of type and key is incorrect

To get PersonalInformation Facet:

IdentifiedContactReference reference = new IdentifiedContactReference("source name", "identifier name");
    var contact = client.Get<Contact>(reference, new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey));
    if (contact != null)
    {
    PersonalInformation existingContactPersonalFacet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey);
     if (existingContactPersonalFacet != null)
      {
       // Write your logic
      }
    }
0

To fetch a custom facet you need to define it into the ContactExpandOptions method. To Update the facet, you need to retrieve the existing facet objects and update the properties.

While fetching the contact form xDB you need to mention with comma separations all the facets which you want to fetch and perform operations like below:

Contact contact = client.Get<Contact>(new IdentifiedContactReference("Your_Source", "Your_Identifier"), new ContactExpandOptions(CustomFacetInformation.DefaultFacetKey, PersonalInformation.DefaultFacetKey,....));
new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey,EmailAddressList.DefaultFacetKey,...)
0

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
                        }
                    }

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.