Add a Contact list using the wrapper class

Participant

Add a Contact list using the wrapper class

Hi, i cant seem to find an example using the api you have to add a new list and then adding contacts to this same list. 

 

I want to use this class $ConstantContact->addList() in order to create it.

2 REPLIES 2
Employee

Hi,

 

You're right, we don't have an example of how to do that and our wrapper doesn't include a search lists feature to easily find a list by name.  Below, I have created an example of creating a new list, and adding a contact to a list.  I have added the contact to list ID 1 regardless of the list that was just created, so you would need to add logic to add the contact to the new list, or find the list ID of the new list and hard code it in.

 

<?php

session_start ();
require_once 'ConstantContact.php';
require_once 'config.php';

$ConstantContact = new ConstantContact("oauth2", $apiKey, $username, $accessToken);

// Get potential contact lists
$lists = $ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50

//Make a new list
$listObj = new ContactList();
$listObj->name = "newListName";
$newList = $ConstantContact->addList($listObj);

// Get potential contact lists
$lists = $ConstantContact->getLists();
// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50

//Insert searching logic to get the newly created list

// Email address here is used for testing purposes
$emailAddress = "testemail11192012@test.com";

// Search for our new Email address
$search = $ConstantContact->searchContactsByEmail($emailAddress);

// If the search didn't return a contact object
if($search == false)
{
	// Create a new Contact Object to store data into
	$contactObj = new Contact();
	// Adding multiple lists to this new Contact Object
	$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
	// Set the email address
	$contactObj->emailAddress = $emailAddress;
	// Create the Contact and DONE
	$Contact = $ConstantContact->addContact($contactObj);
	echo ("Contact added.");

} // Otherwise we update our existing contact
else 
{
	// Gather data from our previous search and store it into a data type
	$contactObj = $ConstantContact->getContactDetails($search[0]);

	// We need to get the old list and add a new list to it as
	// this request requires a PUT and will remove the lists
	// as they are stored in an array
	 array_push($contactObj->lists, $lists['lists'][1]->id );
	
	// Update the contact and DONE
	$UpdateContact = $ConstantContact->updateContact($contactObj);
	echo ("Contact updated.");
}	

?>

 

 

Best Regards,

Shannon W.

API Support Specialist

great work. its working thanks

Developer Portal

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

Visit Page