3

How can we programmatically get the contacts associated to a ContactList by its ContactList Id?

Versions we are using:

  • Sitecore 8.2 (rev. 161221)
  • EXM 4.0.1 (rev. 170105)
  • I followed the above approach. var listManager = (ListManager<ContactList, ContactData>)Factory .CreateObject("contactListManager", true); 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. – Keshava Nov 12 '17 at 2:43
5

Just to clear up a common misconception. EXM uses List Manager to retrieve contact lists and contacts, but they are two different products/APIs. EXM is a module released separately, whereas List Manager is a component released as part of the Sitecore Experience Platform.

So you don't want to look at the EXM API to do what you're asking, but rather the List Manager API.

That being said, to get all the contacts associated to a contact list you do the following:

using Sitecore.Configuration;
using Sitecore.ListManagement;
using Sitecore.ListManagement.ContentSearch.Model;

var listId = Guid.NewGuid();
var listManager = Factory.CreateObject("contactListManager", false) as ListManager<ContactList, ContactData>;
var list = listManager.FindById(listId.ToString());
var contacts = listManager.GetContacts(list);

Keep in mind that you can only do this on a CM role.

0

A refinement of the solution:

var listManager = (ListManager<ContactList, ContactData>)Factory.CreateObject("contactListManager", true);
//here you work with contact.ContactId to get contact object for reading/editing
ContactManager contactManager = (ContactManager)Factory.CreateObject("tracking/contactManager", true);

var list = listManager.FindById("{5A144595-1EDD-4109-A271-25CB13E83503}");
var contacts = listManager.GetContacts(list); //this is how to get all contacts
foreach (ContactData contactData in contacts)
{
   Contact contact = contactManager.LoadContactReadOnly(contactData.ContactId);
}

The code above allows to get a Contact object from the ContactData object provided by the list manager.

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.