Create a personalization rule in Sitecore 9 to display the xConnect Contact Facet in xDB

Akshay Sura - Partner

4 Sep 2018

Share on social media

I made a few more changes to display the points for the user by modifying the TopBarLinksRenderingModel.

In order to setup the custom rule which will compare the users points, use the following code in your project which will evaluate the points:

using System.Linq; using Konabos.XConnect.Loyalty.Model.Facets; using Sitecore.Analytics; using Sitecore.Diagnostics; using Sitecore.Rules; using Sitecore.Rules.Conditions; namespace Feature.Konabos.Loyalty.Website.Sitecore_Customizations.Conditions { public class LoyaltyPointsCondition<T> : IntegerComparisonCondition<T> where T : RuleContext { public int Points { get; set; } protected override bool Execute(T ruleContext) { Assert.ArgumentNotNull(ruleContext, "ruleContext"); Assert.IsNotNull(Tracker.Current, "Tracker.Current is not initialized"); Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session is not initialized"); Assert.IsNotNull(Tracker.Current.Session.Interaction, "Tracker.Current.Session.Interaction is not initialized"); Value = Points; //get value from xDB int userPoints = 0; var xConnectFacets = Tracker.Current.Contact.GetFacet<Sitecore.Analytics.XConnect.Facets.IXConnectFacets>("XConnectFacets"); if (xConnectFacets.Facets != null) { if (xConnectFacets.Facets.Where(f => f.Key == LoyaltyPointsFacet.DefaultFacetKey).FirstOrDefault().Value is LoyaltyPointsFacet loyaltyFacet) { userPoints = loyaltyFacet.PointsEarned; } } return Compare(userPoints); } } }

Next, go to /sitecore/system/Settings/Rules/Definitions/Tags and create a new Tag: Loyalty Points

Create a new element: Loyalty Points in /sitecore/system/Settings/Rules/Definitions/Elements

Add the Loyalty Points Tag to the Selected in /sitecore/system/Settings/Rules/Conditional Renderings/Tags/Default

Once all of this is setup, get to one of your components in the Experience Editor and add a condition which first your need.

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

Sign up to our newsletter

Share on social media

Akshay Sura

Akshay is a nine-time Sitecore MVP and a two-time Kontent.ai. In addition to his work as a solution architect, Akshay is also one of the founders of SUGCON North America 2015, SUGCON India 2018 & 2019, Unofficial Sitecore Training, and Sitecore Slack.

Akshay founded and continues to run the Sitecore Hackathon. As one of the founding partners of Konabos Consulting, Akshay will continue to work with clients to lead projects and mentor their existing teams.


Subscribe to newsletter