Sitecore 8.1 EXM 3.2: How to add Contact to a list in List Manager programatically?

794 Views Asked by At

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?

1

There are 1 best solutions below

0
Mohit Dharmadhikari On

It will get added only after session End.

This is default behavior to limit MongoDB calls.

Also EXM 3.2 version, there were issues with contact list recipients count. You may need to check with sitecore support for hot fix. or alternatively you can upgrade to EXM 3.3