Sitecore Experience Platform

Create a model

This topic describes how to define a custom model.

Create custom facets and events

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

  2. Define facets and events. Ensure that all properties adhere to to xConnect’s type restrictions. Decorate any facet that will be used in the tracker with the [Serializable] attribute.

    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

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

    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.

    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:

    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. Optionally, use the modelbuilder.RegisterType() method to manually register a type. Manual registration is only needed if facets, events, or types inherit a type that is located in a different assembly. For example, Documentation.Model.FrequentFlyerInfo might inherit Common.Model.InheritedBase.

    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();
            }
        }
    }

Deploy your model

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