2

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.

7
  • Do you use your code in any of Sitecore (pre?)request pipelines or in the code of your rendering controller? Do you have Xdb.Enabled and Xdb.Tracking.Enabled? Which of conditions return false for you? Is that contact a returning contact or a new one? Doesn't Tracker.Current.Contact contain what you need? – Marek Musielak May 19 at 6:42
  • Xdb is enabled and I'm calling this function in rendering controller. It is returning contact – Kanniyappan Krishan May 19 at 6:55
  • And why do you try to get contact from XConnectClient? Isn't what you have in Tracker.Current.Contact enough? – Marek Musielak May 19 at 6:57
  • I need xconnect contact. If I'm using Tracker.current.Contact I'm getting cannot implicitly convert type Sitecore. Analytics.tracking.contact to Sitecore.Xconnect.Contact – Kanniyappan Krishan May 19 at 7:19
  • 2
    Please update your question with textual representation of code not screenshot. It's impossible to crawl this question for crawlers properly otherwise. Thanks – Peter Procházka May 19 at 9:19
5

Here is the code which should work for you:

private Contact GetExistingXConnectContact()
{
    if (!Sitecore.Analytics.Tracker.Current.Contact.IsNew)
    {
        var anyIdentifier = Sitecore.Analytics.Tracker.Current.Contact.Identifiers.FirstOrDefault();

        if (anyIdentifier != null)
        {
            using (XConnectClient client = Sitecore.XConnect.Client.Configuration
                .SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    return client.Get(
                        new IdentifiedContactReference(anyIdentifier.Source, anyIdentifier.Identifier),
                        new ContactExpandOptions(PersonalInformation.DefaultFacetKey));
                }
                catch (Exception exc)
                {
                    // handle exc
                }
            }
        }
    }

    return null;
}

You can find more in Sitecore documentation, e.g. https://doc.sitecore.com/developers/90/sitecore-experience-platform/en/set-contact-facets-in-session.html

1

Reference of the Contact should be the same, it should not be like one variable created from Sitecore.Analytics.tracking.contact and another is from Sitecore.Xconnect.Contact

From the above inline comments, I can say your code line Contact contact=null; is taking reference from a different DLL. Could you please change the assignment with

Sitecore.XConnect.Contact contact = null;

In my opinion, it's taking reference from the Sitecore.Analytics.tracking.contact, you can hover on variable and check the reference details.

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.