I am using Sitecore 8.2 - Update 2 and EXM 3.4 - Update 1. I have a list, which I want to use for sending e-mail campaigns. I have a custom form, which I want to use for subscription. I found this article https://himynameistim.wordpress.com/2016/03/01/sitecore-programmatically-adding-contacts-to-a-list/ and I used this part of code:
public void AddContactToList(ContactData contact, ContactList list)
{
ContactListManager listManager = Sitecore.Configuration.Factory.CreateObject("contactListManager", false) as ContactListManager;
List<ContactData> contactList = new List<ContactData>();
contactList.Add(contact);
listManager.AssociateContacts(list, contactList);
}
Problem is, that list is locked after this, so no one else can be added to a list at the time list is locked.
I also tried this code:
var contactRepository = new ContactRepository();
var listManager = Factory.CreateObject("contactListManager", false) as ListManager<ContactList, ContactData>;
var contact = Tracker.Current.Contact;
foreach (var listId in listIds)
{
var contactList = listManager.FindById(listId);
if (contact.Tags.Find("ContactLists") != null && contact.Tags.Find("ContactLists").Values.Select(x => x.Value).Contains(listId))
{
continue;
}
contact.Tags.Set("ContactLists", contactList.Id);
}
var options = new ContactSaveOptions(true, null);
contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
contactRepository.SaveContact(contact, options);
Problem is that a list write warning:
Please note that contacts in the list are currently being indexed, so not all contacts are available to view at this time. 1 out of 0 contacts are currently indexed.
Does anyone have functional method how to add a contact to a list?