Create a custom model

Current version: 10.0

This topic describes how to define a custom model.

Define custom facets and events

To create custom facets and events:

  1. Create a new project - for example, Documentation.Model.

  2. Define facets and events.

    Note

    Ensure that all properties adhere to xConnect’ type restrictions. Decorate any facet used in the tracker with the [Serializable] attribute.

    RequestResponse
    using Sitecore.XConnect;
    using System;
    
    namespace Documentation.Model
    {
        [Serializable]
        [FacetKey(DefaultFacetKey)]
        public class FrequentFlyerInfo : Sitecore.XConnect.Facet
        {
            public const string DefaultFacetKey = "FrequentFlyerInfo";
    
            public FrequentFlyerInfo(string frequentFlyerId)
            {
                FrequentFlyerID = frequentFlyerId;
            }
    
            public FrequentFlyerStatus Status { get; set; }
            public string FrequentFlyerID { get; set; } // Example: ABC12345
    
            public enum FrequentFlyerStatus
            {
                Unknown = 0,
                Bronze,
                Silver,
                Gold
            }
        }
    
        public class BoardFlight : Event
        {
            public static readonly Guid EventDefinitionId = new Guid("2A72E131-BCFD-481C-8E15-04496E9586DC");
    
            public BoardFlight(DateTime timestamp) : base(EventDefinitionId, timestamp)
            {
    
            }
    
            public string Seat { get; set; }
            public bool Upgraded { get; set; }
        }
    }

Create a model

To create a model:

  1. Create a class named CollectionModel - for example, Documentation.Model.CollectionModel. Create an instance of the XdbModelBuilder as shown:

    RequestResponse
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;
    
    namespace Documentation.Model
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();
    
            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));
    
                // Facets and events here
    
                return modelBuilder.BuildModel();
            }
        }
    }
    Note

    The model’s full name (represented by the FullName property of the XdbModel class) includes the model’s name and version. For example: DocumentationModel,1.0.

  2. Optionally, reference another model that you want to use. In the following example, the Sitecore.XConnect.Collection.Model.CollectionModel.Model is referenced. Classes such as Facet and Event are included automatically.

    RequestResponse
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;
    
    namespace Documentation.Model
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();
    
            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));
    
                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
                // Facets and events here
    
                return modelBuilder.BuildModel();
            }
        }
    }
  3. Define the FrequentFlyerInfo contact facet and the BoardFlight event:

    RequestResponse
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;
    
    namespace Documentation
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();
    
            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));
    
                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
    
                modelBuilder.DefineFacet<Contact, FrequentFlyerInfo>(FrequentFlyerInfo.DefaultFacetKey);
                modelBuilder.DefineEventType<BoardFlight>(false);
    
                return modelBuilder.BuildModel();
            }
        }
    }
  4. If facets, events, or types inherit a type that is located in a different assembly, use the modelbuilder.RegisterType() method to manually register a type. For example, Documentation.Model.FrequentFlyerInfo might inherit Common.Model.InheritedBase.

    RequestResponse
    using Sitecore.XConnect;
    using Sitecore.XConnect.Schema;
    using System;
    
    namespace Documentation
    {
        public class CollectionModel
        {
            public static XdbModel Model { get; } = BuildModel();
    
            private static XdbModel BuildModel()
            {
                XdbModelBuilder modelBuilder = new XdbModelBuilder("DocumentationModel", new XdbModelVersion(1, 0));
    
                modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
    
                modelBuilder.RegisterType<Common.Model.InheritedBase>(false);
    
                modelBuilder.DefineFacet<Contact, FrequentFlyerInfo>(FrequentFlyerInfo.DefaultFacetKey);
                modelBuilder.DefineEventType<BoardFlight>(false);
    
                return modelBuilder.BuildModel();
            }
        }
    }
Note

In order to use your model you must deploy your custom model to xConnect, the Marketing Automation Engine, and all core Sitecore instances.

Do you have some feedback for us?

If you have suggestions for improving this article,