The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

C# .NET library, Contact.ContactList always empty


C# .NET library, Contact.ContactList always empty

I'm writing an application using the C# .NET library for the Constant Contact API. I'm able to get all contact lists using GetContactLists(). I'm also able to get all contacts using GetContacts(). I want to iterate through all contacts in a list to perform actions on them if necessary based on some conditions. The first problem I had was there isn't a method to return all contacts within a contact list, something like: GetContactsForContactList(string contactList) So I tried a different way by just retrieving all contacts using GetContacts(), then look at each user's ContactLists property to see if there's a match with the name of the contact list I want, then process accordingly. The problem with this approach is that users have zero items in the ContactLists property, which is a collection of ContactOptInList items. The way I tested is this: using the Constant Contact admin web interface I created a user and added the user to a list. When I retrieve the user in code using the API I can see the user's email, etc., but the ContactLists property has zero items in it. Am I doing something wrong, or is this expected behavior? Is there a different property of the Contact class that I should be evaluating instead to see the contact lists that a user is a member of?

Re: C# .NET library, Contact.ContactList always empty

Hey Yohan,


The reason why it comes back empty is because that the list isnt stored in that general collection but in the details of the contact itself.  That wrapper will do a GET to "{username}/contacts" but if you use GetContactDetails(); then you can get the information you need. 


Please let me know if this helps.

Ryan Davis
Quality Engineer

Re: C# .NET library, Contact.ContactList always empty

Sorry for the formatting of the original message.  I must have had a script blocker on that caused the newlines not to go through, resulting in a single block of text.


And quick correction: I was using GetContactCollection() initially, not "GetContacts()", which was a wrapper method I wrote that calls GetContactCollection().  Sorry for that confusion too.


Thanks for the info.  I ended up using Utility.GetContactDetailsById() instead of the more raw GetContactDetails() and it worked great!  For the resulting contact I drilled down to Contact.ContactLists and was able to grab the ids of the contact lists and match them up against the desired contact list's id.  I wanted to use name instead but the name wasn't populated for each contact list in ContactLists.


The pseudocode is something like this:



for each Contact:

     GetContactDetailsById()      // use Id of current Contact

     for each ContactList:

          if ContactList.Id == Id of desired ContactList then

               add Contact to list of contacts to process afterward

          end if

     end for each

end for each

process list of contacts



I think I'm starting to understand the design of the .NET library you put together, and I think I understand why I was having trouble.  I'm spoiled by LINQ (particularly LINQ to SQL) where you can continually drill down into an object and keep grabbing more and more related data on the fly, for instance drilling down a contact's ContactLists collection and get the Name for each contact list.  Using the CC library there's more manual/explicit fetching of data instead of having it all populated for you.  I'm thinking of writing new wrapper methods like:



... that do that extra fetching for me, because the data structures are there to be filled--they're just not getting filled in a single step.


Anyway, thanks again for your help.

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More