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.

Basic Question with Creating Contact in C#

Highlighted
New Member

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).


 


Thanks!


3 REPLIES 3
Highlighted
Employee

Re: Basic Question with Creating Contact in C#

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

Highlighted
New Member

 Thanks!  That worked.

 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)

Highlighted
Occasional Advisor

Re: account credentials

Hello,


 


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

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
Featured