Akshay Sura - Partner
25 Aug 2018
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. } 12. using Sitecore . XConnect ; 13. 14. namespace Konabos . XConnect . Loyalty . Model . Facets 15. { 16. [ FacetKey ( DefaultFacetKey )] 17. public class LoyaltyPointsFacet : Facet //Contact facet to store the points earned and spent based on the minion processing - read only 18. { 19. public const string DefaultFacetKey = "LoyaltyPointsFacet" ; 20. 21. public int PointsEarned { get ; set ; } 22. public int PointsSpent { get ; set ; } 23. } 24. } 25. using Konabos . XConnect . Loyalty . Model . Facets ; 26. using Sitecore . XConnect ; 27. using Sitecore . XConnect . Schema ; 28. 29. namespace Konabos . XConnect . Loyalty . Model . Models 30. { 31. public class LoyaltyModel 32. { 33. public static XdbModel Model { get ; } = BuildModel (); 34. 35. private static XdbModel BuildModel ( ) 36. { 37. XdbModelBuilder modelBuilder = new XdbModelBuilder ( "LoyaltyModel" , new XdbModelVersion ( 1 , 0 )); 38. 39. modelBuilder . DefineFacet < Contact , LoyaltyPointsFacet >( FacetKeys . LoyaltyPointsFacet ); 40. modelBuilder . DefineFacet < Interaction , LoyaltyOrderInfoFacet >( FacetKeys . LoyaltyOrderInfoFacet ); 41. modelBuilder . DefineFacet < Contact , LoyaltyCommerceFacet >( FacetKeys . LoyaltyCommerceFacet ); 42. 43. modelBuilder . ReferenceModel ( Sitecore . XConnect . Collection . Model . CollectionModel . Model ); 44. 45. return modelBuilder . BuildModel (); 46. } 47. } 48. public class FacetKeys 49. { 50. public const string LoyaltyOrderInfoFacet = "LoyaltyOrderInfoFacet" ; 51. public const string LoyaltyPointsFacet = "LoyaltyPointsFacet" ; 52. public const string LoyaltyCommerceFacet = "LoyaltyCommerceFacet" ; 53. } 54. } 55. using System . IO ; 56. 57. namespace Konabos . XConnect . Console 58. { 59. class Program 60. { 61. static void Main ( string [] args ) 62. { 63. System . Console . WriteLine ( "Generating your model..." ); 64. 65. var model = Konabos . XConnect . Loyalty . Model . Models . LoyaltyModel . Model ; 66. 67. var serializedModel = Sitecore . XConnect . Serialization . XdbModelWriter . Serialize ( model ); 68. 69. File . WriteAllText ( "c:\temp\" + model.FullName + " . json ", serializedModel); 70. 71. System.Console.WriteLine(" Press any key to continue ! Your model is here : " + " c : \temp\ " + model . FullName + " .json " ); 72. System . Console . ReadKey (); 73. } 74. } 75. } 76. using System ; 77. using System . Collections . Generic ; 78. using System . Threading . Tasks ; 79. using Konabos . XConnect . Loyalty . Model . Facets ; 80. using Konabos . XConnect . Loyalty . Model . Models ; 81. using Sitecore . XConnect ; 82. using Sitecore . XConnect . Client ; 83. using Sitecore . XConnect . Client . WebApi ; 84. using Sitecore . XConnect . Collection . Model ; 85. using Sitecore . XConnect . Schema ; 86. using Sitecore . Xdb . Common . Web ; 87. 88. namespace Konabos . XConnect . ConsoleTester 89. { 90. class Program 91. { 92. static void Main ( string [] args ) 93. { 94. Task . Run ( async () => { await ProcessCustomers (); }). Wait (); 95. 96. System . Console . ForegroundColor = ConsoleColor . DarkGreen ; 97. System . Console . WriteLine ( "" ); 98. System . Console . WriteLine ( "END OF PROGRAM." ); 99. System . Console . ReadKey (); 100. } 101. 102. private static async Task ProcessCustomers ( ) 103. { 104. CertificateWebRequestHandlerModifierOptions options = 105. CertificateWebRequestHandlerModifierOptions . Parse ( "StoreName=My;StoreLocation=LocalMachine;FindType=FindByThumbprint;FindValue=BC9B7186102910E8F34EE8D9F38138203F7555BA" ); 106. 107. var certificateModifier = new CertificateWebRequestHandlerModifier ( options ); 108. 109. List < IHttpClientModifier > clientModifiers = new List < IHttpClientModifier >(); 110. var timeoutClientModifier = new TimeoutHttpClientModifier ( new TimeSpan ( 0 , 0 , 20 )); 111. clientModifiers . Add ( timeoutClientModifier ); 112. 113. var collectionClient = new CollectionWebApiClient ( new Uri ( "https://cateringdemo.xconnect.dev.local/odata" ), clientModifiers , new [] { certificateModifier }); 114. var searchClient = new SearchWebApiClient ( new Uri ( "https://cateringdemo.xconnect.dev.local/odata" ), clientModifiers , new [] { certificateModifier }); 115. var configurationClient = new ConfigurationWebApiClient ( new Uri ( "https://cateringdemo.xconnect.dev.local/configuration" ), clientModifiers , new [] { certificateModifier }); 116. 117. var cfg = new XConnectClientConfiguration ( 118. new XdbRuntimeModel ( LoyaltyModel . Model ), collectionClient , searchClient , configurationClient ); 119. 120. try 121. { 122. await cfg . InitializeAsync (); 123. } 124. catch ( XdbModelConflictException ce ) 125. { 126. System . Console . WriteLine ( "ERROR:" + ce . Message ); 127. return ; 128. } 129. 130. using ( var client = new XConnectClient ( cfg )) 131. { 132. try 133. { 134. //search for an existing contact based on an identifier 135. IdentifiedContactReference reference = new IdentifiedContactReference ( "CommerceUser" , "Storefront\sakshay13@gmail.com" ); 136. Contact existingContact = client . Get < Contact >( reference , new ContactExpandOptions ( new string [] { PersonalInformation . DefaultFacetKey , LoyaltyPointsFacet . DefaultFacetKey , LoyaltyCommerceFacet . DefaultFacetKey })); 137. 138. if ( existingContact != null ) 139. { 140. LoyaltyPointsFacet loyaltyPointFacet = existingContact . GetFacet < LoyaltyPointsFacet >( LoyaltyPointsFacet . DefaultFacetKey ); 141. 142. if ( loyaltyPointFacet == null ) 143. { 144. loyaltyPointFacet = new LoyaltyPointsFacet () 145. { 146. PointsEarned = 33 , 147. PointsSpent = 44 148. }; 149. client . SetFacet < LoyaltyPointsFacet >( existingContact , LoyaltyPointsFacet . DefaultFacetKey , loyaltyPointFacet ); 150. 151. client . Submit (); 152. } 153. 154. LoyaltyCommerceFacet loyaltyCommerceFacet = existingContact . GetFacet < LoyaltyCommerceFacet >( LoyaltyCommerceFacet . DefaultFacetKey ); 155. if ( loyaltyCommerceFacet == null ) 156. { 157. loyaltyCommerceFacet = new LoyaltyCommerceFacet () 158. { 159. CommerceCustomerId = "001" 160. }; 161. client . SetFacet < LoyaltyCommerceFacet >( existingContact , LoyaltyCommerceFacet . DefaultFacetKey , loyaltyCommerceFacet ); 162. 163. client . Submit (); 164. } 165. } 166. 167. //Get all contacts and process them 168. //var contacts = client.Contacts.WithExpandOptions(new ContactExpandOptions(PersonalInformation.DefaultFacetKey)).ToEnumerable(); 169. ////var contacts = client.Contacts.ToEnumerable(); 170. //foreach (var contact in contacts) 171. //{ 172. // Console.WriteLine("Contact ID: " + contact.Id.ToString()); 173. 174. // var rsonalInformationFacet = contact.GetFacet<PersonalInformation>(); 175. // PersonalInformation personalInformationFacet = contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey); 176. // if (personalInformationFacet != null) 177. // Console.WriteLine("Contact Name: " + personalInformationFacet.FirstName + " " + personalInformationFacet.LastName); 178. // else 179. // Console.WriteLine("Contact Personal Information not found."); 180. 181. // LoyaltyPointsFacet loyaltyPointFacet = contact.GetFacet<LoyaltyPointsFacet>(LoyaltyPointsFacet.DefaultFacetKey); 182. 183. // if (loyaltyPointFacet == null) 184. // { 185. // Console.WriteLine("Contact Loyalty Information not found."); 186. // LoyaltyPointsFacet visitorInfo = new LoyaltyPointsFacet() 187. // { 188. // PointsEarned = 0, 189. // PointsSpent = 0 190. // }; 191. // client.SetFacet<LoyaltyPointsFacet>(contact, LoyaltyPointsFacet.DefaultFacetKey, visitorInfo); 192. // } 193. // else 194. // Console.WriteLine("Contact Loyalty Found: " + loyaltyPointFacet.PointsEarned); 195. 196. 197. //} 198. 199. Console . ReadLine (); 200. } 201. catch ( XdbExecutionException ex ) 202. { 203. System . Console . WriteLine ( "ERROR:" + ex . Message ); 204. return ; 205. } 206. } 207. } 208. } 209. }
Akshay is a nine-time Sitecore MVP and a two-time Kontent.ai. In addition to his work as a solution architect, Akshay is also one of the founders of SUGCON North America 2015, SUGCON India 2018 & 2019, Unofficial Sitecore Training, and Sitecore Slack.
Akshay founded and continues to run the Sitecore Hackathon. As one of the founding partners of Konabos Consulting, Akshay will continue to work with clients to lead projects and mentor their existing teams.