How to add a contact to a specific list

SOLVED
Go to solution
Frequent Participant

How to add a contact to a specific list

Hi..

I created a new list  ContactList

Added the ContactList to CC  by calling AddList()

 

I then create Contact and call AddContact()

 

How do I add it to specific list?  The contact may already be in another list.

 

thx

 

1 ACCEPTED SOLUTION
Frequent Participant

Hi..

Here's the solution.

Create the list , then get the List ID then assign it back to the contact

 

// Add List
this._constantContact.AddList(contactList);

// Get New List ID
string newContactList_ID = this._constantContact.GetLists(null).Where(l => l.Name == contactList.Name).SingleOrDefault().Id;

contactList.Id = newContactList_ID;

View solution in original post

6 REPLIES 6
Moderator

I'm not sure which SDK you're using so I can't give a specific code example.  However, the Contact object in all SDKs has a Lists collection of some sort (Array, List, etc.).  All you need to do to add a Contact to a new list is add a list object to that collection (append if you want to add to a new list, replace if you want to change list memberships).  Then simply call the update contact method in the SDK to change the list memberships.

Dave Berard
Senior Product Manager, Constant Contact

Hi..

I'm using the CCTWrapper -  .Net SDK Master  - C#

 

I see how you create a New List and a New Contact. But don't see how you associate a new contact to the new list or update / add an existing contact to the New List.

 

AddContact(contact, false);    

 

 

thx again

If you look at the Contact object (https://github.com/constantcontact/.net-sdk/blob/master/CTCTWrapper/Components/Contacts/Contact.cs), you'll notice there the following property:

 

        /// <summary>
        /// Gets or sets the lists.
        /// </summary>
        [DataMember(Name = "lists")]
        public List<ContactList> Lists { get; set; }
 
This property specifies which lists a Contact is on.  You can use the contact.Lists.Add() method to add additional lists prior to updating the Contact.  To update the Contact, you would use the following method:
 
public Contact UpdateContact(string accessToken, string apiKey, Contact contact, bool actionByVisitor)
 
This method is used for updating a single Contact record and can be found in the Contact Service here: https://github.com/constantcontact/.net-sdk/blob/master/CTCTWrapper/Services/ContactService.cs
Dave Berard
Senior Product Manager, Constant Contact
Frequent Participant

Hi..

I now get 

Message = "json.field.missing:#/lists/82/id: Property is required but not found.\r\n"

 

This is what I'm doing.

 

ContactList contactList = new ContactList();
contactList.Name = "TEST"
contactList.Status = "ACTIVE";

 

 

 contact = GetContactByEmailAddress(dr["EMAIL"].ToString().Trim());

 

// Add to new List
contact.Lists.Add(contactList);

 

 result = _constantContact.UpdateContact(contact, false);

 

 

 

any ideas?  thx

You're missing the ID field for the Contact List so we don't know which list you are asking us to reference.  Everything in Constant Contact API is based off IDs for matching.  You could either hard code the ID value for the list you want to add the Contacts to or use the GetLists() method to pull them as part of the application:

 

public IList<ContactList> GetLists(string accessToken, string apiKey, DateTime? modifiedSince)

 

This will return all of the lists and their IDs.  If you want to hard code the lists, just use the method once in a test application, find the ID of the list you want and set it in your ContactList object contactList.Id. 

Dave Berard
Senior Product Manager, Constant Contact
Frequent Participant

Hi..

Here's the solution.

Create the list , then get the List ID then assign it back to the contact

 

// Add List
this._constantContact.AddList(contactList);

// Get New List ID
string newContactList_ID = this._constantContact.GetLists(null).Where(l => l.Name == contactList.Name).SingleOrDefault().Id;

contactList.Id = newContactList_ID;

View solution in original post

Developer Portal

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

Visit Page