Create Custom Facet in Sitecore 9

Sitecore XConnect provides some default collection model to save user data like Personal Information, Email, Address, etc. the complete list you can find in an assembly named Sitecore.XConnect.Collection.Model.dll.

Sometimes as per our business requirement, we need to add some additional information to the collection model with the respective user. In this blog, I will explain how to create a user custom facet model and register it to the Sitecore XDB collection model. You need to perform the below steps to achieve this:

Step 1: Create a Custom Facet Model
[FacetKey(DefaultFacetKey)]
    [Serializable]
    public class CustomFacetInformation : Facet
    {
        public const string DefaultFacetKey = "CustomFacetKey";
        public CustomFacetInformation() { }
        public string CarModel { get; set; }
        public string PassportNumber { get; set; }
        public string PreferredLanguage { get; set; }
    }
Here DefaultFacetKey is facet key name. You can get custom facet values using this key from XDB.
Step 2: Register the Custom Facet Model
After the creation of Custom Facet Model, you need to register it to Sitecore.XConnect.Schema using XdbModelBuilder as below:
public class CustomFacetCollectionModel
    {
        public static XdbModel Model { get; } = CustomFacetCollectionModel.BuilddCustomModel();
        private static XdbModel BuilddCustomModel()
        {
            XdbModelBuilder xdbModelBuilder = new XdbModelBuilder("CustomFacetKeyModel", new XdbModelVersion(1, 0));
            xdbModelBuilder.ReferenceModel(CollectionModel.Model);
            xdbModelBuilder.DefineFacet<Contact, CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
            return xdbModelBuilder.BuildModel();
        }
    }
You can define multiple facets into the same object. Do remember the file name must match the name declared in the model class.
Step 3: Deploy custom Facet model to XDB
To Deploy the custom facet model to XDB you need to serialize the collection model in which you defined your custom facet model. To serialize into JSON prefer Console application to generate the JSON file.
class Program
    {
        static void Main(string[] args)
        {
            var serlizableModel = XdbModelWriter.Serialize(CustomFacetCollectionModel.Model);
            File.WriteAllText(CustomFacetCollectionModel.Model.FullName + ".json", serlizableModel);
        }
    }
On execution of the console application, it will generate a JSON file. Copy that JSON file and paste at the below places:
  • x-connect root path > App_data/Models
  • x-connect root path > App_data/jobs/continuous/IndexWorker/App_data/Models
Step 4: Add Custom Facet Model to Configuration
Once you deploy a custom facet model you need to add or create a patch file for the custom model to the Sitecore.XConnect.Client.Configuration as below:
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <xconnect>
      <runtime type="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration">
        <schemas hint="list:AddModelConfiguration">
          <schema name="SC.Analytics.Collection.Model" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">
            <param desc="modeltype">SC.Foundation.Analytics.Model.CustomFacetCollectionModel, SC.Foundation.Analytics</param>
          </schema>
        </schemas>
      </runtime>
    </xconnect>
  </sitecore>
</configuration>
The original location of the file is /App_Config/Sitecore/XConnect.Client.Configuration/Sitecore.XConnect.Client.config
It will patch custom facet model to the Sitecore and ready to use.
Happy Sitecoreing ðŸ˜Š

Comments

  1. Hi Himmat, as per your suggestion, i have create a patch file and trying to upload a contact with List Manager wizard(OOB contact upload), but my custom facets is not adding into xDB and not getting error logs also

    Please suggest on my issue.

    ReplyDelete
  2. Hello Srik,
    Please email me or connect me over my LinkedIn so that we can look into this in more detail.

    Thanks.

    ReplyDelete
  3. nice informative post. Thanks you for sharing.
    NodeJS Development

    ReplyDelete

Post a Comment

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore Technology MVP Journey 2022