3

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.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
Mohit Dharmadhikari
  • 3,750
  • 2
  • 20
  • 27

1 Answers1

3

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());
Anton
  • 9,682
  • 11
  • 38
  • 68
  • This code is working fine in environment where both cM and CD are same. But throwing Exception in scaled environment. Its unable to get recipient list. newsRecipientList = listRepository.GetEditableRecipientCollection("{my id }"); Any reason you can think of? – Mohit Dharmadhikari Jun 22 '16 at 00:21
  • @MohitDharmadhikari It is hard to say without details about exception. Have you configured "default-cd-cluster" ? – Anton Jun 22 '16 at 08:09
  • yes I have configure the default-cd cluster" as IIS server name (i.e machine name) instead of website domain. Is it fine or do I need to configure it other way – Mohit Dharmadhikari Jun 22 '16 at 09:08
  • the error from the log is listManager value can not be null. So some how its unable to create the list manager object of using factory in cd server. Many thanks for your help mate! Much appreciated – Mohit Dharmadhikari Jun 22 '16 at 10:10
  • I have got detailed log of the error and asked question in new thread. http://stackoverflow.com/questions/37980807/sitecore-exm-list-manger-in-distributed-environment-with-lucene-search-configura/37982755?noredirect=1#comment63445380_37982755 – Mohit Dharmadhikari Jun 24 '16 at 19:01