Create and configure XDB Custom Facet

In this post I am describing the required steps to create a custom facet on XDB and how to use it to personalize the experience…

  • Create your Interface extending IFacet
using System;
using System.Linq;
using Sitecore.Analytics.Model.Framework;

namespace Testing.ContactFacets.Model
    public interface ICRMSegment : IFacet
        string CRMSegmentId { get; set; }
  • Implement your Custom Facet
namespace Testing.ContactFacets.Model
    public class CrmSegment : Facet, ICRMSegment
        private const string FIELD_CRMSegmentId = "CRMSegmentId";
        public CrmSegment()
            base.EnsureAttribute< string>(FIELD_EMPLOYEE_ID);
        public string CRMSegmentId
            get { return base.GetAttribute< string>(FIELD_CRMSegmentId ); }
            set { base.SetAttribute< string>(FIELD_CRMSegmentId , value); }
  • Register your Facet via config file
<configuration xmlns:patch="">
        <element interface="Testing.ContactFacets.Model.ICRMSegment,XXX.AssemblyName " implementation="Testing.ContactFacets.Model.CrmSegment, XXX.AssemblyName" />
            <facet name="CRMSegment" contract="Testing.ContactFacets.Model.ICRMSegment, XXX.AssemblyName" />

  • Create a new XDB Contact
@using Sitecore.Web;
@using Testing.ContactFacets.Model;
@using Sitecore.Analytics.Model.Entities;
 var contact =  Sitecore.Analytics.Tracker.Current.Contact;
 var data = contact.GetFacet<ICRMSegment>("CRMSegment");
 data.CRMSegmentId = "BargainHunter";

var personalInfo = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<Sitecore.Analytics.Model.Entities.IContactPersonalInfo>("Personal");

personalInfo.FirstName = "Stelio";
personalInfo.Surname = "Di Bello";

var contactEmail =  Sitecore.Analytics.Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");

            // this email to show in the Experience Profiles backend.
 if (!contactEmail.Entries.Contains("Home"))
            // set the email
  var email = contactEmail.Entries["Home"];
  email.SmtpAddress = "";
 contactEmail.Preferred = "Home";

CRM data contact facet updated.

Contact ID: @contact.ContactId.ToString() /b>

Employee #: <b> @data.CRMSegmentId </b>

  • Personalize based on CRMSegmentId rule


  • crmsegmentpersonalisation

XDB Contact Rules to personalize based on the facet value

As you are probably familiar, Sitecore 8 architecture is all around XDB and the Contacts.

Arguably Sitecore forgot to ship OOTB personalisation rules based on the Contact facet value of the contacts stored in XDB

Luckily Adam Conn has created two projects

to deliver almost OOTB personalisation based on Contact Facet conditions

Once that you have installed Adaptive rules and Contact Utilities you will be able to personalise based on Contact Facets…. In this example I personalise the content based on the Gender of the Contacts


Personal Facets