Create a custom model
This topic describes how to define a custom model.
Define custom facets and events
To create custom facets and events:
-
Create a new project - for example,
Documentation.Model
. -
Define facets and events.
NoteEnsure that all properties adhere to xConnect’ type restrictions. Decorate any facet used in the tracker with the
[Serializable]
attribute.RequestResponseusing 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:
-
Create a class named
CollectionModel
- for example,Documentation.Model.CollectionModel
. Create an instance of theXdbModelBuilder
as shown:RequestResponseusing 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(); } } }
NoteThe model’s full name (represented by the
FullName
property of theXdbModel
class) includes the model’s name and version. For example:DocumentationModel,1.0
. -
Optionally, reference another model that you want to use. In the following example, the
Sitecore.XConnect.Collection.Model.CollectionModel.Model
is referenced. Classes such asFacet
andEvent
are included automatically.RequestResponseusing 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(); } } }
-
Define the
FrequentFlyerInfo
contact facet and theBoardFlight
event:RequestResponseusing 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(); } } }
-
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 inheritCommon.Model.InheritedBase
.RequestResponseusing 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(); } } }
In order to use your model you must deploy your custom model to xConnect, the Marketing Automation Engine, and all core Sitecore instances.