Skip to main content
3 of 3
added 7 characters in body
Cristi Vulturar
  • 2.3k
  • 2
  • 7
  • 28

Sitecore.XConnect.Contact vs Sitecore.Analytics.Tracking.Contact

I am upgrading code from Sitecore 8.2 to Sitecore 9.3.

In Sitecore 8.2 we have a code snippet:-

public Contact GetCurrentContact()
    {
        if (Tracker.IsActive && Tracker.Current != null && Tracker.Current.Session != null)
        {
            return Tracker.Current.Session.Contact;
        }

        return null;
    }

    public Contact IdentifyContact(global::Sitecore.Security.Accounts.User user)
    {
        if (user == null)
            return null;

        string contactIdentifier = GetContactIdentifier(user);

        if (string.IsNullOrEmpty(contactIdentifier) || contactIdentifier.Contains("anonymous") || Tracker.Current == null || Tracker.Current.Session == null)
        {
            return null;
        }

        Tracker.Current.Contact.Identifiers.AuthenticationLevel = AuthenticationLevel.PasswordValidated;
        Tracker.Current.Session.Identify(contactIdentifier);

        return Tracker.Current.Contact;
    }

In this Sitecore 8.2 Contact object is coming from Sitecore.Analystics.Tracking but in Sitecore 9.3 do you need to convert Contact to Sitecore.XConnect.Contact?

 Contact contact = _contactProvider.GetCurrentContact();

DocLanguageAndRegionPreferences docPreferences;

_contactRepository.SetDocumentLanguageAndRegionPreference(contact, docPreferences);

I have another class using this contact to add item to facet and then communicating with Xdb in Sitecore 8.2

public void SetDocumentLanguageAndRegionPreference(Contact contact, DocLanguageAndRegionPreferences docPreferences)
    {
        if (contact == null)
            return;

        IFacetData facet = contact.GetFacet<IFacetData>(FacetData.FacetName);
        
        if (docPreferences.Languages != null && docPreferences.Languages.Any())
        {
            foreach (var language in docPreferences.Languages)
            {
                var docLanguage = facet.FacetCentralData.Preferences.DocumentLanguageFilters.Create();
                docLanguage.Language = language;
            }
        }
    }

This is Sample code of that class. Facet is working fine, just have some doubt. In this class also I am getting parameter contact in form on Sitecore.Tracking.Analytics.Contact. How can I convert this contact to XConnect.Contact for Sitecore 9.3?