1
votes

I'm working on converting my old Sitecore (< 8) code to work with Sitecore EXM. I'm having a hard time adding users to Recipient Lists from code. The answers in this post: Sitecore 8 EXM add a contact to list from listmanager don't answer my questions completely, and since I cannot comment, I've decided to start a new topic.

My first problem is that my EcmFactory.GetDefaultFactory().Bl.RecipientCollectionRepository.GetEditableRecipientCollection(recipientListId) gives a compilation error on the RecipientCollectionRepository, it says it does not exist. So I've used slightly different code. My code now, is as follows:

        var contactRepository = new ContactRepository();   
        var contactName = this.Email.Text;
        var contact = contactRepository.LoadContactReadOnly(contactName);

        contact = contactRepository.CreateContact(Sitecore.Data.ID.NewID);
        contact.Identifiers.AuthenticationLevel = Sitecore.Analytics.Model.AuthenticationLevel.None;
        contact.System.Classification = 0;
        contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
        contact.Identifiers.Identifier = contactName;
        contact.System.OverrideClassification = 0;
        contact.System.Value = 0;
        contact.System.VisitCount = 0;

        var contactPreferences = contact.GetFacet<IContactPreferences>("Preferences");
        contactPreferences.Language = "nl-NL";

        var contactEmailAddresses = contact.GetFacet<IContactEmailAddresses>("Emails");
        contactEmailAddresses.Entries.Create("test").SmtpAddress = this.Email.Text;
        contactEmailAddresses.Preferred = "test";

        var contactPersonalInfo = contact.GetFacet<IContactPersonalInfo>("Personal");
        contactPersonalInfo.FirstName = contactName;
        contactPersonalInfo.Surname = "recipient";
        if (recipientList != null)
                {
                    var xdbContact = new XdbContactId(contact.ContactId);

                    if (!recipientList.Contains(xdbContact, true).Value)
                    {
                        recipientList.AddRecipient(xdbContact);
                    }

                    contactRepository.SaveContact(contact, new ContactSaveOptions(true, null));
                }

So the recipientList is found, and the first time I add a contact to it, it increases the "Recipients" to 1 (checked using the /sitecore/system/List Manager/All Lists/E-mail Campaign Manager/Custom/RecipientList).

I also have a message which has this Opt-in recipient list, but when I check that message, it says it will be sent to 0 subscribers.

Any thoughts on this?

2

2 Answers

0
votes

See this article listing known issues in Sitecore EXM: https://kb.sitecore.net/articles/149565

"The recipient list shows "0" total recipients after recipients have been subscribed to the list. (62217)"

I got around this in a sandbox environment by adding a simple list (from csv, one contact) to the message. This upped the total recipient count from 0 to 1 which allows the message to be activated. All recipients in the composite list were sent a message.

0
votes

Do you have a distributed environment? If so the RecipientCollectionRepository will not work as it is only available on a Content Management server. You could try using the ClientApi:

ClientApi.UpdateSubscriptions(RecipientId recipientId, string[] listsToSubscribe, string[] listsToUnsubscribe, string managerRootId, bool confirmSubscription)

and just add the id of the list you want to subscribe people to in the first string array.

Just a quick note with this option, listToUnsubscribe does not actually remove a contact from a list. You are meant to pass through the ID of the opt out list. This basically excludes them from any future emails. One draw back is that you will no longer be able to resubscribe them.

If this does not work for you you will need to create your own API between your CD server and your CM server where the CM server uses the recipientCollectionRepository to subscribe and unsubscribe