1

I am creating custom facets in Sitecore 10.2. Below steps I have performed:

  1. Created custom facets.
[FacetKey(DefaultFacetKey)]
    [Serializable]
    public class CustomFacet : Facet
    {
        public const string DefaultFacetKey = "CustomFacet";
        public string ResidentialStatus { get; set; }
    }
  1. Created custom facet model
  public class CustomFacetModel
    {
        public static XdbModel Model { get; } = null;
        static CustomFacetModel()
        {
            Model = BuildModel();
        }

        private static XdbModel BuildModel()
        {
            var builder = new XdbModelBuilder(typeof(CustomFacetModel).FullName, new XdbModelVersion(1, 0));
            builder.DefineFacet<Contact, CustomFacet>(CustomFacet.DefaultFacetKey);
            return builder.BuildModel();
        }
    }
  1. Deployed dll on inetpub/wwwroot/website
  2. Created Json file and pasted on below two places:
{
  "Name": "xx.Foundation.Analytics.Models.CustomFacetModel",
  "Version": "1.0",
  "References": [
    {
      "Name": "XConnect",
      "Version": "1.0"
    }
  ],
  "Types": {
    "xx.Foundation.Analytics.Models.CustomFacet": {
      "Type": "Facet",
      "BaseType": "Sitecore.XConnect.Facet",
      "ClrType": "xx.Foundation.Analytics.Models.CustomFacet, xx.Foundation.Analytics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
      "Properties": {
        "ResidentialStatus": {
          "Type": "String"
        }
      }
    }
  },
  "Facets": [
    {
      "Target": "Contact",
      "Name": "CustomFacet",
      "Type": "xx.Foundation.Analytics.Models.CustomFacet"
    }
  ]
}
inetpub\wwwroot\xxxxconnect.dev.local\App_Data\jobs\continuous\IndexWorker\App_Data\Models
inetpub\wwwroot\xxxlocalxconnect.dev.local\App_Data\Models
  1. Created patch file:
<?xml version="1.0"?>
<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="CustomFacetModel" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">
                        <param desc="modeltype">XX.Foundation.Analytics.Models.CustomFacetModel,XX.Foundation.Analytics</param>
                    </schema>
                </schemas>
            </runtime>
        </xconnect>
    </sitecore>
</configuration>
  1. Saving facets in xDB
  using (var client = SitecoreXConnectClientConfiguration.GetClient())
                {
                   var trackerIdentifier = new IdentifiedContactReference(identifierSource, email);

                    var contact = client.Get(trackerIdentifier, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, CollectionModel.FacetKeys.PhoneNumberList, PhoneNumberList.DefaultFacetKey, CustomFacet.DefaultFacetKey));

                    if (contact == null)
                    {
                       
                        if (Tracker.Current == null && Tracker.Enabled)
                        {
                            Tracker.StartTracking();
                        }
                        if (!Tracker.Enabled || Tracker.Current == null || !Tracker.Current.IsActive)
                        {
                            return false;
                        }
                   }
                }

Tracker.Current is coming null, Tracker.Enabled is true. In log file below error is coming:

25284 14:17:19 ERROR Cannot create tracker.
Exception: System.ArgumentException
Message: The specified type is not a valid facet type.
Parameter name: facetType
Source: Sitecore.Analytics.Model
   at Sitecore.Analytics.Model.Framework.ModelFactory.CreateFacet(Type facetType)
   at Sitecore.Analytics.Model.Framework.Faceted.AddFacet(String name, Type type)
   at Sitecore.Analytics.Model.ContactFactory.Create(ContactCreationParameters parameters, IReadOnlyDictionary`2 facets)
   at Sitecore.Analytics.Data.ContactRepository.CreateContact(ID id)
   at Sitecore.Analytics.Tracking.ContactManager.CreateContact(ID id)
   at Sitecore.Analytics.Pipelines.EnsureSessionContext.LoadContact.Process(InitializeTrackerArgs args)
   at (Object , Object )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
   at Sitecore.Analytics.Pipelines.EnsureSessionContext.EnsureSessionContextPipeline.<>c__DisplayClass4_0.<Run>b__0()
   at Sitecore.Analytics.XConnect.Diagnostics.PerformanceCounters.OperationPerformanceMonitorExtensions.<>c__DisplayClass1_0.<Monitor>b__0()
   at Sitecore.Analytics.XConnect.Diagnostics.PerformanceCounters.OperationPerformanceMonitorExtensions.Monitor[T](OperationPerformanceMonitorBase monitor, Func`1 operation)
   at Sitecore.Analytics.DefaultTracker.EnsureSessionContext()
   at Sitecore.Analytics.Pipelines.CreateTracker.GetTracker.Process(CreateTrackerArgs args)
   at (Object , Object )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
   at Sitecore.Analytics.Tracker.Initialize()

xDB is enabled and SC_ANALYTICS_GLOBAL_COOKIE is coming in browser. Please help.

6
  • Have you deployed Custom facet DLL to xConnect root and IndexWorker? Commented Sep 11, 2022 at 10:29
  • Does it require? Commented Sep 11, 2022 at 11:48
  • yes, it is. doc.sitecore.com/xp/en/developers/102/… Commented Sep 11, 2022 at 12:00
  • @RamanGupta I tried but still the same error Commented Sep 11, 2022 at 14:15
  • 1
    Thanks, Himmat, I forgot to mention builder.ReferenceModel(). Commented Sep 11, 2022 at 17:55

1 Answer 1

2

Can you please confirm what model name you have passed as a parameter while creating the XdbModelBuilder?

XdbModelBuilder xdbModelBuilder = new XdbModelBuilder("{modelname}", new XdbModelVersion(1, 0));

The model name should be identical in the patch file:

      <runtime type="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration">
        <schemas hint="list:AddModelConfiguration">
          <schema name="{modelname}" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">
            <param desc="modeltype">ProjectName.Foundation.Analytics.Model.YourFacetCollectionModel, ProjectName.Foundation.Analytics</param>
          </schema>
        </schemas>
      </runtime>

From your steps mentioned above and the code in your question, all seems good but one more file and piece of code we need to do, could you please confirm that you are doing correct by referring to the below code:

public class YourFacetCollectionModel
    {
        public static XdbModel Model { get; } = BuildCoreModel();
        private static XdbModel BuildCoreModel()
        {
            //double check the below line of code   
            XdbModelBuilder xdbModelBuilder = new XdbModelBuilder("{modelname}", new XdbModelVersion(1, 0));
            //double check the below line of code as well 
            xdbModelBuilder.ReferenceModel(CollectionModel.Model);

            xdbModelBuilder.DefineFacet<Contact, CustomFacetA>(CustomFacetA.DefaultFacetKey);
            xdbModelBuilder.DefineFacet<Contact, CustomFacetB>(CustomFacetB.DefaultFacetKey);

            return xdbModelBuilder.BuildModel();
        }
    }

Your Answer

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

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