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