0

I am upgrading my Sitecore 8.2 code to work with Sitecore 9.1

WE have custom facets and few custom facets have collection field like below code.

 public IElementCollection<IJobPreferenceElement> Jobs 
    {
        get { return GetCollection<IJobPreferenceElement>(_jobs); }
    }
public interface IJobPreferenceElement : IElement
{

 Guid JobId { get; set; }
 string JobName { get; set; }
 int PriorityValue { get; set; }

}

How can we convert above code to work in Sitecore 9.1 (i mean to work with xconnect)?

5

Ones you want to extend your contact model with the facet above, a code will look like below:

JobPreference model:

using System;
using Sitecore.XConnect;
using Sitecore.XConnect.Schema;

namespace {your-namespace}
{
    [Serializable]
    public class JobPreference
    {
        public JobPreference(Guid jobId, string jobName, int priorityValue)
        {
            this.JobId = jobId;
            this.JobName = jobName;
            this.PriorityValue = priorityValue;
        }

        public Guid JobId { get; set; }
        public string JobName { get; set; }
        public int PriorityValue { get; set; }        
    }
}

JobPreferencesList facet definition:

using System;
using Sitecore.XConnect;
using Sitecore.XConnect.Schema;
using System.Collections.Generic;

namespace {your-namespace}
{
    [FacetKey("JobPreferences")]
    [Serializable]
    public class JobPreferencesList : Facet
    {
        public const string DefaultFacetKey = "JobPreferences";

        public JobPreferencesList()
        {
        }

        public Dictionary<string, JobPreference> Jobs { get; set; } = new Dictionary<string, JobPreference>();
    }
}

JobPreferenceModel builder:

using Sitecore.XConnect;
using Sitecore.XConnect.Schema;

namespace {your-namespace}
{
    public class JobPreferenceModel
    {

        public static XdbModel Model { get; } = JobPreferenceModel.BuildModel();

        private static XdbModel BuildModel()
        {
            XdbModelBuilder modelBuilder = new XdbModelBuilder("JobPreferencesList", new XdbModelVersion(1, 0));
                        modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);

            modelBuilder.DefineFacet<Contact, JobPreferencesList>(JobPreferencesList.DefaultFacetKey);

            return modelBuilder.BuildModel();
        }
    }
}

Then create a new console application project in your solution. This app will generate the JSON version of the model, which must be manually copied into your xConnect instance. Reference the following DLLs:

  • Sitecore.XConnect.dll
  • Sitecore.XConnect.Client.dll
  • Sitecore.XConnect.Collection.Model.dll

      using System;
    using System.IO;
    namespace CreateJsonForCustomModel
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine("Generating your model...");
    
                var model = JobPreferenceFacet.JobPreferenceModel.Model;
    
                var serializedModel = Sitecore.XConnect.Serialization.XdbModelWriter.Serialize(model);
    
                File.WriteAllText("c:\\temp\\" + model.FullName + ".json", serializedModel);
    
                System.Console.WriteLine("Press any key to continue! Your model is here: " + "c:\\temp\\" + model.FullName + ".json");
                System.Console.ReadKey();
                Console.ReadLine();
            }
        }
    

Once the json-model file has been created, it should be deployed to:

  • [YOUR PATH TO XCONNECT]\XConnect\App_data\Models\
  • [YOUR PATH TO XCONNECT]\XConnect\App_data\jobs\continuous\IndexWorker\App_data\Models\

In the same time the dll which contains the facet (model) defenicion should be deployed to:

  • [YOUR PATH TO XCONNECT]\XConnect\bin\
  • [YOUR PATH TO XCONNECT]\XConnect\App_data\jobs\continuous\IndexWorker\

The more details example how to build custom facet is provided on Sitecore documentation: Create custom facets and events

| improve this answer | |
  • I am using facets in data migration tool (mongo to sql) then what will be converter type for collection field (Jobs in above example)? – S.Kazmi Feb 6 at 4:05
  • It would be JobPreferences I suppose. But I've never used the data migration tool and might be wrong understood your comment. – Artsem Prashkovich Feb 6 at 14:25

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.