4

I am implementing custom xDB facets from Sitecore 8.2 to Sitecore 9.3. I have a facet called "Data" and ICentralData Inherited from IElemenet. Now IElement is an obsolute in Sitecore 9.3. It is unclear to me how to implement the facet and element in Sitecore 9.3.

public interface ICentralData : IElement
{
    IPreferences Preferences { get; }
    IElementCollection<IFavoriteTool> FavoriteTools { get; }       
}

// Unclear to replace the below methods in Sitecore 9.3 
  GetElement, GetCollection, EnsureElement, EnsureCollection

public class CentralData : Element, ICentralData
{
    private const string PREFERENCES = "Preferences";
    private const string BOOKMARKS = "Bookmarks";       

    public IPreferences Preferences
    {
        get { return GetElement<IPreferences>(PREFERENCES); }
    }

    public IElementCollection<IBookmark> Bookmarks
    {
        get { return GetCollection<IBookmark>(BOOKMARKS); }
    }
    public CentralData()
    {
        this.EnsureElement<IPreferences>(PREFERENCES);
        this.EnsureCollection<IBookmark>(BOOKMARKS);
       
    }

 }

 // would like to understand the replacement of the below methods inside custom facet 
 GetAttribute, SetAttribute, EnsureAttribute

 [Serializable]
public class Data: Facet
{  
    private const string PERSONID = "PersonId";
    private const string centralData = "centralData";

    public int PersonId
    {
        get
        {
            return GetAttribute<int>(PERSONID);
        }
        set
        {
            SetAttribute(PERSONID, value);
        }
    }

    public ICentralData CentralData
    {
        get { return GetElement<ICentralData>(centralData); }
    }

    public Data()
    {
        this.EnsureAttribute<int>(PERSONID);
        this.EnsureElement<ICentralData>(centralData);
    }
  }

As per my understating, I need to create an XConnect custom facet but I'm a bit unclear about Elements and attributes.

Appreciated it anyone can help with this. Thanks in advance!!

2 Answers 2

2

Sitecore XConnect provides some default collection models to save user data like Personal Information, Email, Address, etc. the complete list you can find in an assembly named Sitecore.XConnect.Collection.Model.dll.

You need to perform the below-mentioned steps to create custom facets in Sitecore.

  1. Create a Custom Facet Model
  2. Register the Custom Facet Model
  3. Deploy custom Facet model to XDB
  4. Add Custom Facet Model to Configuration

You can find step by step details about the above points from here: create-custom-facet-model-in-sitecore-9

Now next point is once you created the Custom facets then How will you use those? For the same you can follow the below blog:

work-with-custom-facet-in-Sitecore

Let me know in case you find any trouble, I am glad to help you.

2
  • Thanks for your response. I have one query in DefineFacet. I am using multiple facets model in my project like below CustomFacetInformation1:Facet CustomFacetInformation2:Facet CustomFacetInformation3:Facet Can we define the facet like this xdbModelBuilder.DefineFacet<Contact, CustomFacetInformation1>(CustomFacetInformation1.DefaultFacetKey); xdbModelBuilder.DefineFacet<Contact, CustomFacetInformation2>(CustomFacetInformation2.DefaultFacetKey); xdbModelBuilder.DefineFacet<Contact, CustomFacetInformation2>(CustomFacetInformation3.DefaultFacetKey); May 11, 2021 at 12:34
  • When you want to perform operation on multiple facet key then you need to define all facet in ContactExpmandOptions with comma separate like Sitecore.XConnect.ContactExpandOptions(CustomFacetInformation1.DefaultFacetKey, CustomFacetInformation2.DefaultFacetKey)); May 11, 2021 at 13:17
3

From Sitecore 9 onwards, creating a facet model is changed, you need to re-write your model class for it, below are the sample points you need to consider -

  • Create a class that inherits Sitecore.XConnect.Facet, let take an example from your code and name it Data

  • Add one or more properties to it and if you are going to use it in the context of the tracker you need to decorate it with [Serializable]

  • Use a constructor to indicate mandatory properties and also all facets must declare an empty constructor - this is an OData restriction. The constructor can be private.

  • Decorate facet or facet properties with the [PIISensitive] or [DoNotIndex] attributes if necessary.

  • Optionally, use the [FacetKey] attribute to define a default facet key. Default facet keys simplify usage of the xConnect Client API. Facet keys are case-sensitive.

You can see the Sitecore 93 documentation for creating custom facet - https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/create-a-custom-facet.html

You don't think about Elements and attributes, in this case, your facet model will look like below -

[Serializable]
[FacetKey(DefaultFacetKey)]
public class Data : Facet
{
    public const string DefaultFacetKey = "Data";

    public int PersonId { get; set; }

    public CentralData CentralData { get; set; }

    public Data()
    {
    }
}
public class CentralData
{
    private Preferences Preferences { get; set; }
    private List<FavoriteTool> FavoriteTools { get; set; }
    public List<Bookmark> Bookmarks { get; set; }
}

I have created this on the fly and you may need to update this model as per the logic of the existing Sitecore 8.2 facet.

Once you will create a facet model you need to define it in your collection model using the .DefineFacet() method. See the Define a custom facet in the model. If you do not register your facet, you cannot use it. You will get errors such as The type of this instance does not correspond to any type in the schema.

Also If you are using the model in a Sitecore context, the model class must be added to the client configuration file - https://doc.sitecore.com/developers/93/sitecore-experience-platform/en/create-a-custom-model.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.