cancel
Showing results for 
Search instead for 
Did you mean: 

Get Contact List Members Code Snippet

Administrator

Step 1) Retrieve the set of contacts for a particular list.

 

Step 2) Find the id of the contact you are interested in.

Step 1 of 2: Retrieve the Set of Contacts from a Particular List

 

Here is the sample code retrieve the list of members in a given list.

 

   Header locationHeader;
// URL to get all of the "members" for list id 32
GetMethod httpGet = new GetMethod("http://api.constantcontact.com/ws/customers/joesflowers/lists/32/members");
try {
int status = httpClient.executeMethod(httpGet);
if (status >= 400){
throw new RuntimeException("HTTP request failed with status: " + httpGet.getStatusText() + ". message: " + httpGet.getStatusLine());
}
//response XML can be parsed using parser of choice.
System.out.println(httpGet.getResponseBodyAsString());
} catch (Exception ex) {
throw new RuntimeException( "Caught exception while GETting " + httpGet.getURI(), ex);
} finally {
// release any connection resources used by the method
httpGet.releaseConnection();
}

The response body will look something like this:

 

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://api.constantcontact.com/ws/customers/joesflowers/lists/1/members</id>
<title type="text">Contacts for Customer: joesflowers</title>
<link href="http://api.constantcontact.com/ws/customers/joesflowers/lists/1/members" />
<link href="http://api.constantcontact.com/ws/customers/joesflowers/lists/1/members" rel="self" />
<author>
<name>joesflowers</name>
</author>
<updated>2009-05-14T18:23:24.271Z</updated>
<link href="/ws/customers/joesflowers/lists/1/members" rel="first" />
<link href="/ws/customers/joesflowers/lists/1/members" rel="current" />
<entry>
<link href="/ws/customers/joesflowers/lists/1/members/26" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/26</id>
<title type="text">Contact: ssg@sg.cc</title>
<updated>2009-05-14T18:23:26.692Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<ContactListMember xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/26">
<EmailAddress>ssg@sg.cc</EmailAddress>
<Name></Name>
</ContactListMember>
</content>
</entry>
<entry>
<link href="/ws/customers/joesflowers/lists/1/members/25" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/25</id>
<title type="text">Contact: sg23@sg.cc</title>
<updated>2009-05-14T18:23:26.693Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<ContactListMember xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/25">
<EmailAddress>sg23@sg.cc</EmailAddress>
<Name>ss</Name>
</ContactListMember>
</content>
</entry>
<entry>
<link href="/ws/customers/joesflowers/lists/1/members/24" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/24</id>
<title type="text">Contact: sgddd@sg.cc</title>
<updated>2009-05-14T18:23:26.694Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<ContactListMember xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/24">
<EmailAddress>sgddd@sg.cc</EmailAddress>
<Name>MAss</Name>
</ContactListMember>
</content>
</entry>
</feed>

 

The next step is to retrieve the contact using the Contact resource by finding Contact Id.

 

Step 2 of 2: Find the id of the Contact You Are Interested In

Here is Java sample code to retrieve the details of the contact from the above list. The above xml can be parsed to find the contact id, which can be used to retreive the contact's details. In the example above, contact id is http://api.f1.constantcontact.com/ws/customers/joesflowers/contacts/26 and email address is ssg@sg.cc: 

 

   Header locationHeader;
// URL to get all of the "sends" events for contact id 32 (John's Contact ID)
GetMethod httpGet = new GetMethod("http://api.constantcontact.com/ws/customers/joesflowers/contacts/26");
try {
int status = httpClient.executeMethod(httpGet);
if (status >= 400){
throw new RuntimeException("HTTP request failed with status: " + httpGet.getStatusText() + ". message: " + httpGet.getStatusLine());
}
//response XML can be parsed using parser of choice.
System.out.println(httpGet.getResponseBodyAsString());
} catch (Exception ex) {
throw new RuntimeException("Caught exception while GETting " + httpGet.getURI(), ex);
} finally {
// release any connection resources used by the method
httpGet.releaseConnection();
}

The xml result of the above code would be similar to the following:

 

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts</id>
<title type="text">Contacts for Customer: joesflowers</title>
<link href="http://api.constantcontact.com/ws/customers/joesflowers/contacts" />
<link href="http://api.constantcontact.com/ws/customers/joesflowers/contacts" rel="self" />
<author>
<name>joesflowers</name>
</author>
<updated>2008-10-16T13:43:52.342Z</updated>
<link href="/ws/customers/joesflowers/contacts" rel="first" />
<link href="/ws/customers/joesflowers/contacts" rel="current" />
<entry>
<link href="/ws/customers/joesflowers/contacts/26" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/26</id>
<title type="text">Contact: sg@sg.cc</title>
<updated>2008-10-16T13:43:52.360Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/26>
<Status>Active</Status>
<EmailAddress>sg@sg.cc</EmailAddress>
<EmailType>HTML</EmailType>
<Name></Name>
<OptInTime>2008-09-09T14:44:14.233Z</OptInTime>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<Confirmed>false</Confirmed>
<InsertTime>2008-09-09T14:44:14.233Z</InsertTime>
</Contact>
</content>
</entry>
</feed>

webservices