Sitecore XConnect provides some default collection models to save user data like Personal Information, Email, addresses, 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
usingSitecore.XConnect;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;// install Sitecore.XConnect nuget package.namespaceMA.CustomFacets.Model{[Serializable][FacetKey(DefaultFacetKey)]publicclassCustomerStatus : Facet{publicstringStatus {get;set; }publicconststringDefaultFacetKey ="CustomerStatus";}}
- Here DefaultFacetKey is the 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 the Custom Facet Model, you need to register it to Sitecore.XConnect.Schema using XdbModelBuilder as below:
usingSitecore.XConnect;usingSitecore.XConnect.Schema;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;// install Sitecore.XConnect.Collection.Model nuget package.namespaceMA.CustomFacets.Model{publicstaticclassCustomerCollectionModel{publicstaticXdbModel Model {get; } = CreateModel();privatestaticXdbModel CreateModel(){XdbModelBuilder builder =newXdbModelBuilder("CustomFacets.Xconnect.CustomerCollectionModel",newXdbModelVersion(1, 0));builder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);builder.DefineFacet<Contact, CustomerStatus>(CustomerStatus.DefaultFacetKey);returnbuilder.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.
usingMA.CustomFacets.Model;usingSystem;usingSystem.IO;// install Sitecore.XConnect nuget package// add reference to MA.CustomFacets projectnamespaceMA.CustomFacets.GenerateModel{classProgram{staticvoidMain(string[] args){varmodel = Sitecore.XConnect.Serialization.XdbModelWriter.Serialize(CustomerCollectionModel.Model);File.WriteAllText(CustomerCollectionModel.Model.FullName +".json", model);Console.WriteLine("Completed");Console.Read();}}}
- 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
Also, deploy your Custom facet class project DLL at the following places:
- <x-connect root path>\App_data\jobs\continuous\AutomationEngine\
- <x-connect root path>\App_data\jobs\continuous\IndexWorker\
- <x-connect root path>\bin
- <Instance_Name>.sc\bin
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:
<?xmlversion="1.0"encoding="utf-8"?><configurationxmlns:patch="http://www.sitecore.net/xmlconfig/"xmlns:set="http://www.sitecore.net/xmlconfig/set/"><sitecore><xconnect><runtimetype="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration"><schemashint="list:AddModelConfiguration"><schemaname="CustomCollectionModel"type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration"patch:after="schema[@name='collectionmodel']"><paramdesc="modeltype">MA.CustomFacets.Model.CustomerCollectionModel, MA.CustomFacets</param></schema></schemas></runtime></xconnect></sitecore></configuration>
Step 5: Deploy the model to the Marketing Automation Engine:
- Create a configuration file named sc.Demo.CustomModel.xml in
<XConnectRootPath>\App_Data\jobs\continuous\AutomationEngine\App_Data\Config\sitecore\MarketingAutomation_patch The file name must start with sc. and end with .XML.
Paste the following model configuration and make the following changes:
<Settings> <Sitecore> <XConnect> <Services> <XConnect.Client.Configuration> <Options> <Models> <CustomCollectionModel> <TypeName>MA.CustomFacets.Model.CustomerCollectionModel, MA.CustomFacets</TypeName> </CustomCollectionModel> </Models> </Options> </XConnect.Client.Configuration> </Services> </XConnect> </Sitecore></Settings>
Note: You’d need to create a new folder named MarketingAutomation_patch under AutomationEngine\App_Data\Config\sitecore to store all the patch files under one folder.
At this path :<XConnectRootPath>\App_Data\jobs\continuous\AutomationEngine\App_Data\Config\sitecore\MarketingAutomation_patch,
Create the sc.MarketingAutomation.ContactLoader.xml and register the custom facet default key. (if this file is not already there, create one)
<Settings> <!-- Marketing Automation contact loader configuration--> <Sitecore> <XConnect> <MarketingAutomation> <Engine> <Services> <MarketingAutomation.Loading.ContactFacetsConfigurator> <Options> <IncludeFacetNames> <PhoneNumbers>PhoneNumbers</PhoneNumbers> <CustomerStatus>CustomerStatus</CustomerStatus> </IncludeFacetNames> </Options> </MarketingAutomation.Loading.ContactFacetsConfigurator> </Services> </Engine> </MarketingAutomation> </XConnect> </Sitecore></Settings>- Now your Custom facet is ready for use.
https://sitecorewithraman.wordpress.com/2020/08/02/sitecore-xconnect-custom-facets-part-i/
ReplyDelete