We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Basic Question with Creating Contact in C#

Occasional Participant

Basic Question with Creating Contact in C#

 I believe this is very basic, but I can't figure out the issue.  I have a list called "Test".  I get an error when I do the following code in C#

<code>Utility.IsValidUserAuthentication(ad); string[] emailAddress = new string[] {"test@gmail.com"}; string nextChunkId; IList<ConstantContactBO.Contact> list = Utility.SearchContactByEmail(ad, emailAddress, out nextChunkId); if (list.Count == 0) { ConstantContactBO.Contact contact = new ConstantContactBO.Contact("test@gmail.com", "Sal", "Smith"); ConstantContactBO.ContactOptInList cl = new ConstantContactBO.ContactOptInList(); cl.OptInSource = ConstantContactBO.ContactOptSource.ActionByContact; cl.ContactList = new ConstantContactBO.ContactList("Test"); contact.ContactLists.Add(cl); Utility.CreateNewContact(ad, contact); } else { Console.Write("Sal is already there"); }</code>

 I get this error:


System.Collections.Generic.KeyNotFoundException: The given key was not present i

n the dictionary.

   at System.ThrowHelper.ThrowKeyNotFoundException()

   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)

   at ConstantContactUtility.Components.ContactComponent.CreateAtomEntry(Contact

 contact, String accountContactListUri)

   at ConstantContactUtility.Utility.CreateNewContact(AuthenticationData authent

icationData, Contact contact)

   at ConstantContactSyncPOC1.Program.Main(String[] args)


My authentication is fine - what do I have wrong?  I have tried several different ways of refering to the list, but can't figure out what the ID is (used its sort order, but that gave same error).




Sorry for the long response time, this post fell under the radar and I certainly apologize for that.

I just needed to modify the Contact List to use an actual list id (ie: 1, 2, 18 ..etc). You can obtain the list Id by performing an http get on https://api.constantcontact.com/ws/customers/{user-name}/lists, and it will be referred to in the <id> node.  I also needed to make a slight modification to the syntax that selects the contact Opt In Source.


  1.             Utility.IsValidUserAuthentication(ad);

  2.             string emailAddress = new string { "test@gmail.com" };

  3.             string nextChunkId;

  4.             IList<ConstantContactBO.Contact> list = Utility.SearchContactByEmail(ad, emailAddress, out nextChunkId);

  5.             if (list.Count == 0)

  6.             {

  7.                 ConstantContactBO.Contact contact = new ConstantContactBO.Contact("test@gmail.com", "Sal", "Smith");

  8.                 ConstantContactBO.ContactOptInList cl = new ConstantContactBO.ContactOptInList();

  9.                 contact.OptInSource = ContactOptSource.ActionByContact;

  10.                 cl.ContactList = new ConstantContactBO.ContactList("1");

  11.                 contact.ContactLists.Add(cl);

  12.                 Utility.CreateNewContact(ad, contact);

  13.             }

  14.             else

  15.             {

  16.                 Console.Write("Sal is already there");

  17.             }

I hope this helps resolve your issue. I certainly apologize for any inconvenience this cuased. Please let me know if you have any other questions or concerns regarding this. Thank you.

David J

Occasional Participant

 Thanks!  That worked.  


With regards to the http get - what do I enter for username / password?  I tried retrieving that manually usering regular account credentials, and it did not work (invalid access)

Regular Participant



The credentials you would use for all API calls would be the following:

Username: {apikey}%{ConstantContactusername}

Password: {ConstantContactPassword}



Andrew T

Support Engineer, Constant Contact

Developer Portal

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

Visit Page