Accessing facets in session


How to access contact facets that have been loaded into session from xConnect.

When a visitor returns to your site, a configurable list of facets are loaded into session from xConnect.


You can edit facets that are loaded into session, but they are not automatically saved to xConnect on session end. You must use the xConnect Client API to update facets.

The IXConnectFacets tracker facet inherits Sitecore.Analytics.Model.Framework.IFacet and contains a dictionary of xConnect facets that inherit Sitecore.XConnect.Facet. Configuration determines which facets are loaded into the facets dictionary. The following example demonstrates how to access the PersonalInformation facet, which is available by default:

var xConnectFacet = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<Sitecore.Analytics.XConnect.Facets.IXConnectFacets>("XConnectFacets");

PersonalInformation personal = xConnectFacet.Facets[PersonalInformation.DefaultFacetKey] as PersonalInformation;


In Sitecore 9.3 and later, legacy facet classes such as IContactPersonalInfo are no longer available. Use the IXConnectFacets class instead.

You can uncomment <facet> elements in the App_Config\Sitecore\Marketing.xDB\Sitecore.Analytics.Model.config configuration file to re-enable legacy facets. However, we recommend that you upgrade to IXConnectFacets.

The following example demonstrates how to access the key behavior cache:

var xConnectFacets = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<IXConnectFacets>("XConnectFacets");

KeyBehaviorCache kbc = xConnectFacets.Facets[KeyBehaviorCache.DefaultFacetKey] as KeyBehaviorCache;
var recentCampaigns = kbc.Campaigns;


Each key behavior cache entry has a DefinitionId property. Use the Marketing Operations API to retrieve definition items by ID.

The following example demonstrates how to access the contact behavior profile:

var xConnectFacets = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<IXConnectFacets>("XConnectFacets");

ContactBehaviorProfile cbp = xConnectFacets.Facets[ContactBehaviorProfile.DefaultFacetKey] as ContactBehaviorProfile;
var matchedProfileIDs = cbp.Scores.Select(x => x.Value.ProfileDefinitionId);


From Sitecore 10.1, the calculation logic for the contact behavior profile has changed. By default, the older calculation logic is disabled. To enable it, see Enable the Contact Behavior Profile with the most recent profile scores. The ProfileScores facet and the ProfileScores property in the InteractionCache facet is obsolete.

The following example demonstrates how to access a contact’s automation activities:

var xConnectFacets = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<IXConnectFacets>("XConnectFacets");

AutomationPlanEnrollmentCache enrolments = xConnectFacets.Facets[AutomationPlanEnrollmentCache.DefaultFacetKey] as AutomationPlanEnrollmentCache;

var specificActivityEnrolments = enrolments.ActivityEnrollments.Where(x => x.ActivityId == Guid.Parse("9098fa76-eaf3-4adc-ada5-e46014ba9f30"));
var specificPlanEnrolments = enrolments.ActivityEnrollments.Where(x => x.AutomationPlanDefinitionId == Guid.Parse("6ce0925e-961e-469e-b085-454a6ba0a931"));


Each automation plan enrollment cache entry has a plan definition ID and an activity ID. Use the Automation Plan Definition manager to retrieve the plan definition item. Use the Activity Descriptor Locator API to retrieve activity descriptors by ID.