3
votes

I am trying to add existing sitecore user to the recipient list. I have created empty recipient list using list manager. Below is my code.

RecipientId recipient = new SitecoreUserName(userProfile.UserName);
     var listRepository = new ListManagerCollectionRepository();
        var newsRecipientList = listRepository.GetEditableRecipientCollection("{list-id}");
    if (!newsRecipientList.Contains(recipient).Value)
    {
          newsRecipientList.AddRecipient(recipient);
    }

However when I see my recipient list it is always empty. Please Help.

1

1 Answers

3
votes

Sitecore Email Experience Manager works with List Manager. List Manager works with Contacts entities, not Users entities. That is why user can be present in Sitecore, but you could have problems with adding him to list: contact could be absent. Also I am not sure that RecipientId for list could be get from SitecoreUserName as it is in your example.

Other issue that could cause this problem: Sitecore writes contacts to database not immediately. You could have contact in memory(e. g. Tracker.Current.Session.Contact), but it could be accessible for others only after Session end when it will be added to database and unlocked.

You could resolve your issue by using Brian Pedersen approach. It works for me.

var repository = new ExtendedContactRepository();
var contact = Repository.GetOrCreateContact(userEmail);
recipientList.AddRecipient(contact.ContactId.ToID());