I am trying to update & query an xDB contact via the XConnect Client API from a console application.
I can query and retrieve the default facets e.g. PersonalInformation.FirstName PersonalInformation.LastName as expected.
I have created a custom facet following the tutorial 5 on the sitecore docs.
However when I try and retrieve my custom facet, it is throwing an exception:
contact.GetFacet<SportType>(SportType.DefaultKey);
When I look at the contacts facets, all the custom models are binding to UntypedFacet:
When I retrieve and update these Facets on my Sitecore instance, it works successfully. It just wont work via the Client API.
My environment is running on the Sitecore HabitatHome 9.3 local environment.
Console API Code
var reference = new ContactReference(Guid.Parse("b483f033-8875-0000-0000-05c029e74ad5"));
var contact = client.Get<Contact>(reference, new ContactExpandOptions(SportName.DefaultKey,SportType.DefaultKey,
CustomFacetInformation.DefaultFacetKey, PersonalInformation.DefaultFacetKey) { });
var customFacet = contact.GetFacet<CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
if(customFacet == null)
{
var customFacetInformation = new CustomFacetInformation()
{
CarModel = "Mazda",
PassportNumber = "XYZ123",
PreferredLanguage = "English"
};
client.SetFacet(contact, CustomFacetInformation.DefaultFacetKey, customFacetInformation);
}
var sportType = contact.GetFacet<SportType>(SportType.DefaultKey);
Custom Facet Classes:
namespace Sitecore.HabitatHome.Global.Website.Facets
{
[FacetKey(DefaultFacetKey)]
[Serializable]
public class CustomFacetInformation : Facet
{
public const string DefaultFacetKey = "CustomFacetInformation";
public CustomFacetInformation() { }
public string CarModel { get; set; }
public string PassportNumber { get; set; }
public string PreferredLanguage { get; set; }
}
public class CustomFacetCollectionModel
{
public static XdbModel Model { get; } = CustomFacetCollectionModel.BuilddCustomModel();
private static XdbModel BuilddCustomModel()
{
XdbModelBuilder xdbModelBuilder = new XdbModelBuilder("CustomFacetCollectionModel", new XdbModelVersion(1, 0));
xdbModelBuilder.ReferenceModel(CollectionModel.Model);
xdbModelBuilder.DefineFacet<Contact, CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
return xdbModelBuilder.BuildModel();
}
}
}
Sitecore Config:
<xconnect>
<runtime type="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration">
<schemas hint="list:AddModelConfiguration">
<schema name="customFacetCollectionModel" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">
<param desc="modeltype">Sitecore.HabitatHome.Global.Website.Facets.CustomFacetCollectionModel, Sitecore.HabitatHome.Global.Website</param>
</schema>
</schemas>
</runtime>
</xconnect>
Facet Model: CustomFacetCollectionModel, 1.0.json
{
"Name": "CustomFacetCollectionModel",
"Version": "1.0",
"References": [
{
"Name": "XConnect",
"Version": "1.0"
},
{
"Name": "Sitecore.XConnect.Collection.Model",
"Version": "9.3"
}
],
"Types": {
"Sitecore.HabitatHome.Global.Website.Facets.CustomFacetInformation": {
"Type": "Facet",
"BaseType": "Sitecore.XConnect.Facet",
"ClrType": "Sitecore.HabitatHome.Global.Website.Facets.CustomFacetInformation, Sitecore.HabitatHome.Global.Website, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"Properties": {
"CarModel": {
"Type": "String"
},
"PassportNumber": {
"Type": "String"
},
"PreferredLanguage": {
"Type": "String"
}
}
}
},
"Facets": [
{
"Target": "Contact",
"Name": "CustomFacetInformation",
"Type": "Sitecore.HabitatHome.Global.Website.Facets.CustomFacetInformation"
}
]
}