0

in every article and documentation I can see they are using for one facet type one (default) facet key.

MyCustomFacet facet = new MyCustomFacet();
var facetReference = new FacetReference(contact, MyCustomFacet.DefaultFacetKey);
client.SetFacet(facetReference, facet);
client.Submit();

Can I have (for this example) several 'MyCustomFacet' types under different facet keys?

MyCustomFacet facetA = new MyCustomFacet();
var facetReferenceA = new FacetReference(contact, "MyCustomFacetKeyA");
client.SetFacet(facetReferenceA, facetA);

MyCustomFacet facetB = new MyCustomFacet();
var facetReferenceB = new FacetReference(contact, "MyCustomFacetKeyB");
client.SetFacet(facetReferenceB, facetB);

MyCustomFacet facetC = new MyCustomFacet();
var facetReferenceC = new FacetReference(contact, "MyCustomFacetKeyC");
client.SetFacet(facetReferenceC, facetC);

client.Submit(); // Exception "Unknown facet 'MyCustomFacetKeyA' for Contact"

I am asking because it's not working for me.

Thanks in advance

Lukas

5

Yes, you can have several 'MyCustomFacet' types under different facet keys. Can you please try like this, it will work for you:

 MyCustomFacet facetA = contact.GetFacet<MyCustomFacet>(MyCustomFacet.DefaultFacetKey);
        facetA = new MyCustomFacet
        {
            propertyA = "valueA",
            propertyB = "valueB"
            //and so on
        };
        client.SetFacet(contact, MyCustomFacet.DefaultFacetKey, facetA);
        client.Submit();

Please ensure that FacetKeys are registered into the customModel.json(you have created for custom facet). e.g.

"Facets": [
{
  "Target": "Contact",
  "Name": "MyCustomFacetKeyA",
  "Type": "dllName.MyCustomFacetModelA"
},
{
  "Target": "Contact",
  "Name": "MyCustomFacetKeyB",
  "Type": "dllName.MyCustomFacetModelB"
},
| improve this answer | |
  • So as I understand, I have to register these keys before I will use them? I wanted to know if I can dynamically create these keys. Imagine facet key could be "MyCustomFacetKey + Guid.NewGuid()" – Lukas Dvorak May 3 '19 at 12:55
  • Yes, we need to create a custom model class for CustomFacet with custom facet key. Then we deploy our custom model using Builder Model class. It creates and serializes our custom facet model and we need to manually copy that custom JSON model to the xConnect location. After that, you need to create a patch file or register your DLL and class name with namespace into a schema tag into Sitecore.XConnect.Client.config. – Himmat Singh Dulawat May 3 '19 at 13:10
  • It's too long so I have added another answer. – Himmat Singh Dulawat May 3 '19 at 13:17
5

Yes, we need to create a custom model class for CustomFacet with custom facet key. Then we deploy our custom model and serializes our custom facet model and we need to copy that custom JSON model to the xConnect location. After that, you need to create a patch file or register your DLL and class name with namespace into a schema tag into Sitecore.XConnect.Client.config. For more detail: please refer below links that are useful:

1) https://doc.sitecore.com/developers/90/sitecore-experience-platform/en/create-a-custom-facet.html

2) https://doc.sitecore.com/developers/91/sitecore-experience-platform/en/deploy-a-custom-model.html

3) https://sitecorerockz.wordpress.com/2018/09/24/create-custom-facet-in-xconnect/

Please do remember facetkeys are case sensitive.

Regarding Dynamic key: I haven't played with the dynamic key but I think it would not be good to play with dynamic as we need to register and deploy all keys and during the set or update facets and we can't remember those keys during operations.

Suggestion for Dynamic facet keys:

I want to suggest instead of creating multiple dynamic facet keys, create one single facet key and make one property into that facet model in which assign/ map your Product Catalogue as a JSON object, like:

{
"title":{"text":"Product Catalogue"},
"Types":[
           {"name":"Product A","data":["Market",20.00]},
           {"name":"Product B","data":["Sales",30.00]},
           {"name":"Product C","data":["Operating",40.00]},
           {"name":"Product D","data":["Gross",10.00]}
         ] }
| improve this answer | |
  • Yes yes, I know how to register it.. So it's not possible to do it in dynamic way. My idea was to collect some external data related to user - to have one facet with collection of "dynamic" keys and then by needs to ask for specific data with dynamic facet key. You can imagine it with product catalogue where data are not comming from Sitecore.. – Lukas Dvorak May 3 '19 at 18:29
  • 2
    Hi Lukas, I want to suggest instead of creating multiple dynamic facet keys, create one single facet key and make one property into that facet model in which assign/ map your Product Catalogue as a JSON object, like: { "title":{"text":"Product Catalogue"}, "Types":[ {"name":"Product A","data":["Market",20.00]}, {"name":"Product B","data":["Sales",30.00]}, {"name":"Product C","data":["Operating",40.00]}, {"name":"Product D","data":["Gross",10.00]} ] } – Himmat Singh Dulawat May 4 '19 at 8:05
  • 1
    Hi Himmat, that's what I finally did :-) Thanks for suggestions! – Lukas Dvorak May 6 '19 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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