I am working with xDB code upgrade from Sitecore 8.2 to 9.3 xconnect. I'm using the below code in 8.2 to get current contact and it returning contact value.
Public Contact Getcurrentcontact()
{
return Tacker.Current.Session.Contact
}
To do the same in Sitecore 9.3 I am using the below code to get contact by contact id but it always returns the null value.
public Contact GetCurrentContact()
{
if (Tracker.IsActive && Tracker.Current != null && Tracker.Current.Session != null)
{
Contact contact = null;
Guid identifier = Tracker.Current.Contact.ContactId;
ContactReference reference = new ContactReference(identifier);
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
{
contact = client.Get<Contact>(reference,
new ContactExpandOptions(PersonalInformation.DefaultFacetKey,
EmailAddressList.DefaultFacetKey));
}
return contact;
}
return null;
}
Can someone please help.
Xdb.Enabled
andXdb.Tracking.Enabled
? Which of conditions return false for you? Is that contact a returning contact or a new one? Doesn'tTracker.Current.Contact
contain what you need?XConnectClient
? Isn't what you have inTracker.Current.Contact
enough?