Creating a Contact Facet in xDB Sitecore 9 & xConnect Magic!

In this video I am going to walk you through how to create a contact facet in xDB and use xConnect to add that facet value for an existing contact in Sitecore 9 Update 2.

Useful links:

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

    1. using Sitecore.XConnect;
    2.  
    3. namespace Konabos.XConnect.Loyalty.Model.Facets
    4. {
    5. public class LoyaltyCommerceFacet : Facet //Contact facet to store the Commerce CustomerId - read only
    6. {
    7. public const string DefaultFacetKey = "LoyaltyCommerceFacet";
    8.  
    9. public string CommerceCustomerId { get; set; }
    10. }
    11. }
    1. using Sitecore.XConnect;
    2.  
    3. namespace Konabos.XConnect.Loyalty.Model.Facets
    4. {
    5. [FacetKey(DefaultFacetKey)]
    6. public class LoyaltyPointsFacet : Facet //Contact facet to store the points earned and spent based on the minion processing - read only
    7. {
    8. public const string DefaultFacetKey = "LoyaltyPointsFacet";
    9.  
    10. public int PointsEarned { get; set; }
    11. public int PointsSpent { get; set; }
    12. }
    13. }
    1. using Konabos.XConnect.Loyalty.Model.Facets;
    2. using Sitecore.XConnect;
    3. using Sitecore.XConnect.Schema;
    4.  
    5. namespace Konabos.XConnect.Loyalty.Model.Models
    6. {
    7. public class LoyaltyModel
    8. {
    9. public static XdbModel Model { get; } = BuildModel();
    10.  
    11. private static XdbModel BuildModel()
    12. {
    13. XdbModelBuilder modelBuilder = new XdbModelBuilder("LoyaltyModel", new XdbModelVersion(1, 0));
    14.  
    15. modelBuilder.DefineFacet<Contact, LoyaltyPointsFacet>(FacetKeys.LoyaltyPointsFacet);
    16. modelBuilder.DefineFacet<Interaction, LoyaltyOrderInfoFacet>(FacetKeys.LoyaltyOrderInfoFacet);
    17. modelBuilder.DefineFacet<Contact, LoyaltyCommerceFacet>(FacetKeys.LoyaltyCommerceFacet);
    18.  
    19. modelBuilder.ReferenceModel(Sitecore.XConnect.Collection.Model.CollectionModel.Model);
    20.  
    21. return modelBuilder.BuildModel();
    22. }
    23. }
    24. public class FacetKeys
    25. {
    26. public const string LoyaltyOrderInfoFacet = "LoyaltyOrderInfoFacet";
    27. public const string LoyaltyPointsFacet = "LoyaltyPointsFacet";
    28. public const string LoyaltyCommerceFacet = "LoyaltyCommerceFacet";
    29. }
    30. }
    1. using System.IO;
    2.  
    3. namespace Konabos.XConnect.Console
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. System.Console.WriteLine("Generating your model...");
    10.  
    11. var model = Konabos.XConnect.Loyalty.Model.Models.LoyaltyModel.Model;
    12.  
    13. var serializedModel = Sitecore.XConnect.Serialization.XdbModelWriter.Serialize(model);
    14.  
    15. File.WriteAllText("c:\temp\" + model.FullName + ".json", serializedModel);
    16.  
    17. System.Console.WriteLine("Press any key to continue! Your model is here: " + "c:\temp\" + model.FullName + ".json");
    18. System.Console.ReadKey();
    19. }
    20. }
    21. }
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Threading.Tasks;
    4. using Konabos.XConnect.Loyalty.Model.Facets;
    5. using Konabos.XConnect.Loyalty.Model.Models;
    6. using Sitecore.XConnect;
    7. using Sitecore.XConnect.Client;
    8. using Sitecore.XConnect.Client.WebApi;
    9. using Sitecore.XConnect.Collection.Model;
    10. using Sitecore.XConnect.Schema;
    11. using Sitecore.Xdb.Common.Web;
    12.  
    13. namespace Konabos.XConnect.ConsoleTester
    14. {
    15. class Program
    16. {
    17. static void Main(string[] args)
    18. {
    19. Task.Run(async () => { await ProcessCustomers(); }).Wait();
    20.  
    21. System.Console.ForegroundColor = ConsoleColor.DarkGreen;
    22. System.Console.WriteLine("");
    23. System.Console.WriteLine("END OF PROGRAM.");
    24. System.Console.ReadKey();
    25. }
    26.  
    27. private static async Task ProcessCustomers()
    28. {
    29. CertificateWebRequestHandlerModifierOptions options =
    30. CertificateWebRequestHandlerModifierOptions.Parse("StoreName=My;StoreLocation=LocalMachine;FindType=FindByThumbprint;FindValue=BC9B7186102910E8F34EE8D9F38138203F7555BA");
    31.  
    32. var certificateModifier = new CertificateWebRequestHandlerModifier(options);
    33.  
    34. List<IHttpClientModifier> clientModifiers = new List<IHttpClientModifier>();
    35. var timeoutClientModifier = new TimeoutHttpClientModifier(new TimeSpan(0, 0, 20));
    36. clientModifiers.Add(timeoutClientModifier);
    37.  
    38. var collectionClient = new CollectionWebApiClient(new Uri("https://cateringdemo.xconnect.dev.local/odata"), clientModifiers, new[] { certificateModifier });
    39. var searchClient = new SearchWebApiClient(new Uri("https://cateringdemo.xconnect.dev.local/odata"), clientModifiers, new[] { certificateModifier });
    40. var configurationClient = new ConfigurationWebApiClient(new Uri("https://cateringdemo.xconnect.dev.local/configuration"), clientModifiers, new[] { certificateModifier });
    41.  
    42. var cfg = new XConnectClientConfiguration(
    43. new XdbRuntimeModel(LoyaltyModel.Model), collectionClient, searchClient, configurationClient);
    44.  
    45. try
    46. {
    47. await cfg.InitializeAsync();
    48. }
    49. catch (XdbModelConflictException ce)
    50. {
    51. System.Console.WriteLine("ERROR:" + ce.Message);
    52. return;
    53. }
    54.  
    55. using (var client = new XConnectClient(cfg))
    56. {
    57. try
    58. {
    59. //search for an existing contact based on an identifier
    60. IdentifiedContactReference reference = new IdentifiedContactReference("CommerceUser", "Storefront\sakshay13@gmail.com");
    61. Contact existingContact = client.Get<Contact>(reference, new ContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey, LoyaltyPointsFacet.DefaultFacetKey, LoyaltyCommerceFacet.DefaultFacetKey }));
    62.  
    63. if (existingContact != null)
    64. {
    65. LoyaltyPointsFacet loyaltyPointFacet = existingContact.GetFacet<LoyaltyPointsFacet>(LoyaltyPointsFacet.DefaultFacetKey);
    66.  
    67. if (loyaltyPointFacet == null)
    68. {
    69. loyaltyPointFacet = new LoyaltyPointsFacet()
    70. {
    71. PointsEarned = 33,
    72. PointsSpent = 44
    73. };
    74. client.SetFacet<LoyaltyPointsFacet>(existingContact, LoyaltyPointsFacet.DefaultFacetKey, loyaltyPointFacet);
    75.  
    76. client.Submit();
    77. }
    78.  
    79. LoyaltyCommerceFacet loyaltyCommerceFacet = existingContact.GetFacet<LoyaltyCommerceFacet>(LoyaltyCommerceFacet.DefaultFacetKey);
    80. if (loyaltyCommerceFacet == null)
    81. {
    82. loyaltyCommerceFacet = new LoyaltyCommerceFacet()
    83. {
    84. CommerceCustomerId = "001"
    85. };
    86. client.SetFacet<LoyaltyCommerceFacet>(existingContact, LoyaltyCommerceFacet.DefaultFacetKey, loyaltyCommerceFacet);
    87.  
    88. client.Submit();
    89. }
    90. }
    91.  
    92. //Get all contacts and process them
    93. //var contacts = client.Contacts.WithExpandOptions(new ContactExpandOptions(PersonalInformation.DefaultFacetKey)).ToEnumerable();
    94. ////var contacts = client.Contacts.ToEnumerable();
    95. //foreach (var contact in contacts)
    96. //{
    97. // Console.WriteLine("Contact ID: " + contact.Id.ToString());
    98.  
    99. // var rsonalInformationFacet = contact.GetFacet<PersonalInformation>();
    100. // PersonalInformation personalInformationFacet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey);
    101. // if (personalInformationFacet != null)
    102. // Console.WriteLine("Contact Name: " + personalInformationFacet.FirstName + " " + personalInformationFacet.LastName);
    103. // else
    104. // Console.WriteLine("Contact Personal Information not found.");
    105.  
    106. // LoyaltyPointsFacet loyaltyPointFacet = contact.GetFacet<LoyaltyPointsFacet>(LoyaltyPointsFacet.DefaultFacetKey);
    107.  
    108. // if (loyaltyPointFacet == null)
    109. // {
    110. // Console.WriteLine("Contact Loyalty Information not found.");
    111. // LoyaltyPointsFacet visitorInfo = new LoyaltyPointsFacet()
    112. // {
    113. // PointsEarned = 0,
    114. // PointsSpent = 0
    115. // };
    116. // client.SetFacet<LoyaltyPointsFacet>(contact, LoyaltyPointsFacet.DefaultFacetKey, visitorInfo);
    117. // }
    118. // else
    119. // Console.WriteLine("Contact Loyalty Found: " + loyaltyPointFacet.PointsEarned);
    120.  
    121.  
    122. //}
    123.  
    124. Console.ReadLine();
    125. }
    126. catch (XdbExecutionException ex)
    127. {
    128. System.Console.WriteLine("ERROR:" + ex.Message);
    129. return;
    130. }
    131. }
    132. }
    133. }
    134. }