I have ContactForm with checkbox to subscribe to a newsletter.
I need to check if subscribing person is already a sitecore contact, update this person's data and add contact to RecipientsList in List Manager.
Here is my code:
var recipientCollectionRepository = RecipientCollectionRepository.GetDefaultInstance();
var list = recipientCollectionRepository.GetEditableRecipientCollection(listId);
var contactRepository = new ContactRepository();
var contact = contactRepository.LoadContactReadOnly(ContactEmail);
if (contact != null)
{
if (list != null)
{
var xdbContact = new XdbContactId(contact.ContactId);
if (!list.Contains(xdbContact, true).Value)
{
list.AddRecipient(xdbContact);
}
}
}
else
{
contact = contactRepository.CreateContact(Sitecore.Data.ID.NewID);
contact.Identifiers.AuthenticationLevel = Sitecore.Analytics.Model.AuthenticationLevel.None;
contact.Identifiers.Identifier = ContactEmail;
contact.Tags.Add("ContactLists",listId);
var contactEmailAddresses = contact.GetFacet<IContactEmailAddresses>("Emails");
if (!contactEmailAddresses.Entries.Contains("Email"))
{
contactEmailAddresses.Entries.Create("Email").SmtpAddress = ContactEmail;
contactEmailAddresses.Preferred = "Email";
}
var contactPersonalInfo = contact.GetFacet<IContactPersonalInfo>("Personal");
contactPersonalInfo.FirstName = ContactFirstName;
contactPersonalInfo.Surname = ContactSurname;
if (list != null)
{
var xdbContact = new XdbContactId(contact.ContactId);
if (!list.Contains(xdbContact, true).Value)
{
list.AddRecipient(xdbContact);
}
contactRepository.SaveContact(contact, new ContactSaveOptions(true, null));
}
}
Although I can find it in mongoDB screen here I cannot see it on my list in ListManager.
What else I need to do to be able to see my new contact on the list in ListManager?