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
using
Sitecore.XConnect;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
// install Sitecore.XConnect nuget package.
namespace
MA.CustomFacets.Model
{
[Serializable]
[FacetKey(DefaultFacetKey)]
public
class
CustomerStatus : Facet
{
public
string
Status {
get
;
set
; }
public
const
string
DefaultFacetKey =
"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:
using
Sitecore.XConnect;
using
Sitecore.XConnect.Schema;
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
// install Sitecore.XConnect.Collection.Model nuget package.
namespace
MA.CustomFacets.Model
{
public
static
class
CustomerCollectionModel
{
public
static
XdbModel Model {
get
; } = CreateModel();
private
static
XdbModel CreateModel()
{
XdbModelBuilder builder =
new
XdbModelBuilder(
"CustomFacets.Xconnect.CustomerCollectionModel"
,
new
XdbModelVersion(1, 0));
builder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
builder.DefineFacet<Contact, CustomerStatus>(CustomerStatus.DefaultFacetKey);
return
builder.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.
using
MA.CustomFacets.Model;
using
System;
using
System.IO;
// install Sitecore.XConnect nuget package
// add reference to MA.CustomFacets project
namespace
MA.CustomFacets.GenerateModel
{
class
Program
{
static
void
Main(
string
[] args)
{
var
model = 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:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
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
=
"CustomCollectionModel"
type
=
"Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration"
patch:after
=
"schema[@name='collectionmodel']"
>
<
param
desc
=
"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