Peter Kjaer (SDL)

December 1, 2011

Looping through Contacts

Filed under: Audience Manager — Tags: , , , , , , — Peter Kjaer @ 8:35 AM

When working with Contacts in Audience Manager, you will often need to loop through a list of Contacts and perform some action.

There’s a couple of things you need to know about that, which are not immediately obvious:

  1. Any list of Contacts is streamed from the database. This means that your database connection is open while you loop over the Contacts.
  2. Due to a limitation in the objects we use to stream the Contacts, you cannot do another database operation while they are being streamed.
  3. Not all of the data for a Contact is loaded.
  4. For most lists, only the first 1000 Contacts will be returned.


Most of those points are for performance reasons, so that the system is able to handle millions of Contacts.
If you just need to show a list of Contacts (i.e. like the GUI does), you get a very fast list.


But if you need more data or you need to perform an operation on each Contact, you should first get the list from the API, place them in a collection of your own, and then do another loop through that collection.


For example, to read the creation date of each Contact:

ContactFilter filter = new ContactFilter(UserContext.Current);

// Create a copy of the streamed list
List<Contact> contacts = new List<Contact>(Contact.GetContacts(UserContext.Current, filter, null));

foreach(Contact unloadedContact in contacts)
{
    // Re-load each Contact to access all of its data
    Contact contact = new Contact(unloadedContact.Id, UserContext.Current);
    DateTime creationDate = contact.VersionInfo.CreationDate;
    [...]
}


Or to delete Contacts within an Address Book (this time using LINQ’s ToList method):

ContactFilter filter = new ContactFilter(UserContext.Current);

// Create a copy of the streamed list
List<Contact> contactsToDelete = Contact.GetContacts(UserContext.Current, filter, addressBook).ToList();

// Loop over the new list and delete each Contact
foreach (Contact contact in contactsToDelete)
{
    contact.Delete();
}

…where “addressBook” is an instance of type AddressBook.


Advertisements

3 Comments »

  1. […] Most of those points are for performance reasons, so that the system is able to handle millions of Contacts. Peter Kjaer (SDL) […]

    Pingback by SDL Tridion WCM by rhertel - Pearltrees — December 7, 2011 @ 12:49 AM

  2. Hi,
    I’m trying to delete a Contact from Audience Manager using the Tridion.OutboundEmail.ContentDelivery.Profile Contact object but it has no delete. How did you get the delete method?

    Comment by Neil Highley — June 20, 2012 @ 6:23 PM

    • There is no delete method on the Content Delivery side.
      You’ll need to delete it on the Content Management side.

      Comment by Peter Kjaer — June 20, 2012 @ 7:49 PM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: