5

I have a requirement to get Contact data from a Registration page and save that data into a List Manager in Sitecore.

I followed the below approach.

//Create ListManager Object
var listManager = (ListManager)Factory.CreateObject("contactListManager", true);

//Find ListManager by Id so that later I can associate a contact to it.
var list = listManager.FindById("{5A144595-1EDD-4109-A271-25CB13E83503}");

But while finding the listmanager by id I am getting Duplicate Ids Not allowed error. (I am taking Id from Sitecore Item)

I searched in almost all sites for resolving this issue but could not find a solution. Appreciate any help on this.

3

Here is a blog post describing what you need:

Sitecore - proper way of adding contacts to contact lists .

You should reference Sitecore.EmailCampaign.dll and use

Sitecore.Modules.EmailCampaign.ClientApi.UpdateSubscriptions

You need to fill email, firstname and lastname of your contact first.

So your code can look like this:

string managerRoot = ID_OF_THE_ITEM /sitecore/content/Email Campaign;
string contactList = "{697E3130-0BEF-4DD7-C8EB-C165D70D604A}";

if (!Sitecore.Analytics.Tracker.IsActive)
    Sitecore.Analytics.Tracker.StartTracking();

var currentContact = Sitecore.Analytics.Tracker.Current.Contact;

var personal = currentContact.GetFacet<Sitecore.Analytics.Model.Entities.IContactPersonalInfo>("Personal");

personal.FirstName = SOME_FIRST_NAME;
personal.Surname = SOME_LAST_NAME;

var emailAddresses = currentContact.GetFacet<Sitecore.Analytics.Model.Entities.IContactEmailAddresses>("Emails");
if (!emailAddresses.Entries.Contains("Email"))
{
    emailAddresses.Entries.Create("Email");
}

var email = emailAddresses.Entries["Email"];
email.SmtpAddress = SOME_EMAIL_ADDRESS;
emailAddresses.Preferred = "Email";

var recipientId = new Sitecore.Modules.EmailCampaign.Xdb.XdbContactId(Sitecore.Analytics.Tracker.Current.Contact.ContactId);
Sitecore.Modules.EmailCampaign.ClientApi.UpdateSubscriptions(recipientId, new[] { contactList }, new string[] { }, managerRoot, false);

More information can be found here The Email Experience Manager Client API

You should not use contact.Tags.Set and contact.Tags.Remove for adding contact to a contact list. This can cause problems with list locking and list recipients count not being updated correctly.

  • Hi Marek, Thank you for your suggestions. I followed the approach but when using UpdateSubscriptions it is giving the following error : The item ID: {BC799B34-8423-48AC-A2FE-D128E6300659} could not find the Email Experience Manager Root it requested. The Item ID used is the List Root ID. Tried with List Manager Folder ID as well but got the same error. Appreciate any suggestions on that? – Keshava Nov 13 '17 at 12:18
  • I assume you have EXM installed, correct? ManagerRootId should be the ID of the /sitecore/content/Email Campaign item – Marek Musielak Nov 13 '17 at 13:41
  • @Keshava Is this a single instance setup or a scaled setup? I.e. do you have a dedicated CD and CM server? If you do not, you do not need EXM to add a contact to a contact list. – Jacob Nielsen Nov 13 '17 at 23:47
  • Hi Marek, Thank you very much for your support. It works now. You rockk! – Keshava Nov 15 '17 at 12:14
  • Marek, Thank you once again. Just had one more question. Can we add a single contact into multiple contact lists? I tried adding the same contact into 2 Lists by writing this code twice and changing the contact list ID : Sitecore.Modules.EmailCampaign.ClientApi.UpdateSubscriptions(recipientId, new[] { contactList }, new string[] { }, managerRoot, false); But it was taking only the first one. Any suggestion on this? Or is it that we can link a single contact to only one Contact List? – Keshava Nov 15 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.