Work with Custom Facet in Sitecore 9
In this blog, you can find details about how to set, fetch and update custom
facet models to XDB. To know about how to create a custom facet model please
find my previous blog here.
Facet will be saved with respect to the Contact, whether it is new or
existing contact. If contact does not exist, then create a new contact and
add it to XDB if it exists then get that contact first.
I am starting this blog by setting new custom facet properties, later
fetching the existing facet and updating the properties, and saving it to
XDB. You need to use client.SetFacet() method to set or update facet.
- Add a new custom facet:
In this following example, I am adding the CustomFacetInformation facet on contact. This contact does yet not have the CustomFacetInformation facet. You need to create a new object of facet then assign
properties and set it to contact.
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient()) { try { Contact contact = client.Get<Contact>(new IdentifiedContactReference("Your_Source", "Your_Identifier"), new ContactExpandOptions()); if (contact != null) { CustomFacetInformation customFacetInformation = new CustomFacetInformation() { CarModel = "Baleno", PassportNumber = "PAS34567NUM890", PreferredLanguage = "English" }; client.SetFacet(contact, CustomFacetInformation.DefaultFacetKey, customFacetInformation); } else { //create new contact and set facet properties Contact contact = new Contact(new ContactIdentifier("Identifier", "Source", ContactIdentifierType.Known)); client.AddContact(contact); // Facet is new var newFacet = new customFacetInformation { CarModel = "Baleno", PassportNumber = "PAS34567NUM890", PreferredLanguage = "English" } client.SetFacet(contact, "CustomExmFacet", newFacet); } client.Submit(); } catch (XdbExecutionException) { // Exception } }
- Fetch and update custom facet:
To fetch a custom facet you need to define it into the ContactExpandOptions method. To Update the facet, you need to retrieve the existing facet
object and update the properties. In this below example I am updating the
existing CustomFacetInformation facet on contact:
// Get contact from xConnect, update and save the facet
using (XConnectClient client = SitecoreXConnectClientConfiguration.GetClient())
{
{
try
{
Contact contact = client.Get<Contact>(new IdentifiedContactReference("Your_Source", "Your_Identifier"),
new ContactExpandOptions(CustomFacetInformation.DefaultFacetKey));
if (contact != null)
{
// Retrieve facet by name
var facet = contact.GetFacet<CustomFacetInformation>(CustomFacetInformation.DefaultFacetKey);
if (facet != null)
{
facet.CarModel = "Audi";
facet.PreferredLanguage = "German";
client.SetFacet(contact, CustomFacetInformation.DefaultFacetKey, facet);
}
else
{
//Create new facet for Custom model and SetFacet
}
client.Submit();
}
}
catch (XdbExecutionException ex)
{
// Exception handling
}
}
}
To verify it connect to your database check in XDB_Shard0 or XDB_Shard1 database, using the below query:
SELECT * FROM
[Helixbase_Xdb.Collection.Shard0].[xdb_collection].[ContactFacets] where
FacetKey=’CustomFacetKey’
SELECT * FROM
[Helixbase_Xdb.Collection.Shard1].[xdb_collection].[ContactFacets] where
FacetKey=’CustomFacetKey’
Here “Helixbase”
is my Sitecore instance name replace it with yours.
You can find your custom facet values in the FacetData column in JSON
format.
Do Remember:
- If you are adding a facet first time then you don’t need to mention the facet key into ContactExpandOptions
- If you are updating an existing facet, then you have to mention the facet key into ContactExpandOptions
In my next blog, I will let you know about “How to display Custom Facet on
Experience Profile Tab”.
Happy Sitecoreing
Nice blog ..
ReplyDeleteWhen we can expect another blog on "How to display Custom Facet on Experience Profile Tab”?
Hi, when we use client.add () method then we are not able to save other sitecore provided facets i.e. EngagementMeasures
ReplyDeleteExmKeyBehaviorCache
InteractionsCache
KeyBehaviorCache
You need to mentions all those key as below:
ReplyDeleteSitecore.XConnect.ContactExpandOptions(EmailAddressList.DefaultFacetKey, AddressList.DefaultFacetKey));
Great post.
ReplyDeletehttps://myanimelist.net/profile/MuwaffaqAltaf
Going to graduate school was a positive decision for me. I enjoyed the coursework, the presentations, the fellow students, and the professors. And since my company reimbursed 100% of the tuition, the only cost that I had to pay on my own was for books and supplies. Otherwise, I received a free master’s degree. All that I had to invest was my time. Help to Buy Slough
ReplyDeleteThis is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck. Help to Buy Slough
ReplyDeleteGreat post.
ReplyDeletehttps://fairygodboss.com/users/profile/-glJQNcvPC/aftab-kasem