private static async Task UpdateContactFacet(XConnectClientConfiguration xConfig, string contactId, string firstName, string lastName, string email, string countryCode, string mobileNumber, string customerStatus) { // Initialize a client using the validated configuration using (var client = new XConnectClient(xConfig)) { try { Contact contact; //IdentifiedContactReference reference; //ContactReference reference; if (!string.IsNullOrEmpty(contactId)) { var reference = new Sitecore.XConnect.ContactReference(Guid.Parse(contactId)); contact = client.Get <Contact>(reference, new ContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey, PhoneNumberList.DefaultFacetKey, CustomerFacets.DefaultFacetKey })); } else { IdentifiedContactReference reference = new IdentifiedContactReference("CustomerRef", email); contact = client.Get <Contact>(reference, new ContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey, PhoneNumberList.DefaultFacetKey, CustomerFacets.DefaultFacetKey })); } if (contact != null) { var customerFacet = contact.GetFacet <CustomerFacets>(CustomerFacets.DefaultFacetKey); if (customerFacet != null) { // Change facet properties customerFacet.CustomerStatus = customerStatus; //Update facet in contact client.SetFacet(contact, customerFacet); } else { // Facet is new CustomerFacets newCustomerFacet = new CustomerFacets() { CustomerStatus = customerStatus }; client.SetFacet(contact, newCustomerFacet); } PersonalInformation existingContactPersonalFacet = contact.GetFacet <PersonalInformation>(PersonalInformation.DefaultFacetKey); if (existingContactPersonalFacet != null) { existingContactPersonalFacet.FirstName = firstName; existingContactPersonalFacet.LastName = lastName; client.SetFacet(contact, PersonalInformation.DefaultFacetKey, existingContactPersonalFacet); } else { PersonalInformation newPersonalFacet = new PersonalInformation() { FirstName = firstName, LastName = lastName }; client.SetFacet(contact, PersonalInformation.DefaultFacetKey, newPersonalFacet); } PhoneNumberList existingContactPhonFacet = contact.GetFacet <PhoneNumberList>(PhoneNumberList.DefaultFacetKey); if (existingContactPhonFacet != null) { existingContactPhonFacet.PreferredPhoneNumber = new PhoneNumber(countryCode, mobileNumber); existingContactPhonFacet.PreferredKey = "Mobile"; client.SetFacet(contact, PhoneNumberList.DefaultFacetKey, existingContactPhonFacet); } else { PhoneNumberList newContactPhonFacet = new PhoneNumberList(new PhoneNumber(countryCode, mobileNumber), "Mobile"); client.SetFacet(contact, PhoneNumberList.DefaultFacetKey, newContactPhonFacet); } await client.SubmitAsync(); } Console.ReadLine(); } catch (XdbExecutionException ex) { // Deal with exception } } }
private static async Task CreateContacts(XConnectClientConfiguration xConfig, string firstName, string lastName, string email, string countryCode, string mobileNumber, string customerStatus) { DateTime DateOfBirth = new DateTime(1947, 12, 9); // Initialize a client using the validated configuration using (var client = new XConnectClient(xConfig)) { try { IdentifiedContactReference reference = new IdentifiedContactReference("CustomerRef", email); Contact existingContact = client.Get <Contact>(reference, new ContactExpandOptions(new string[] { PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey, PhoneNumberList.DefaultFacetKey, CustomerFacets.DefaultFacetKey })); if (existingContact != null) { Console.WriteLine("This contact already exists.Please run the update option."); } else { var identifiers = new ContactIdentifier[] { new ContactIdentifier("CustomerRef", $"{email}", ContactIdentifierType.Known) }; // Create a new contact with the identifier Contact newContact = new Contact(identifiers); PersonalInformation personalInfoFacet = new PersonalInformation(); personalInfoFacet.FirstName = firstName; personalInfoFacet.LastName = lastName; personalInfoFacet.Birthdate = DateOfBirth; client.SetFacet <PersonalInformation>(newContact, PersonalInformation.DefaultFacetKey, personalInfoFacet); EmailAddressList newEmailFacet = new EmailAddressList(new EmailAddress(email, true), "Work"); client.SetFacet <EmailAddressList>(newContact, newEmailFacet); PhoneNumberList newPhoneFacet = new PhoneNumberList(new PhoneNumber(countryCode, mobileNumber), "Mobile"); client.SetFacet <PhoneNumberList>(newContact, newPhoneFacet); CustomerFacets customerFacets = new CustomerFacets(); customerFacets.CustomerStatus = customerStatus; client.SetFacet <CustomerFacets>(newContact, CustomerFacets.DefaultFacetKey, customerFacets); client.AddContact(newContact); // Submit contact await client.SubmitAsync(); // Get the last batch that was executed var operations = client.LastBatch; Console.WriteLine("RESULTS..."); //// Loop through operations and check status foreach (var operation in operations) { Console.WriteLine(operation.OperationType + operation.Target.GetType().ToString() + " Operation: " + operation.Status); } } } catch (XdbExecutionException ex) { // Deal with exception } } }