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.

Creating a contact using a C#.Net solution.

SOLVED
Go to solution
Highlighted
Occasional Participant

Creating a contact using a C#.Net solution.

We are trying to link a contact form on our website to a contact list in our Constant Contact account. We are utilizing the C# library to accomplish this. 

 

After the form is submitted, this is called: 

 

addContact(txtEmail.Text.Trim(), txtName.Text.Trim());

 

Method below:

 

 

 private void addContact(string email, string name){
        string[] cc_email = new string[] { email };
        string nextChunkId;
        IList<Contact> list = Utility.SearchContactByEmail(AuthenticationData, cc_email, out nextChunkId);
        if (list.Count == 0)
        {
            Contact contact = GetContactInformation(email, name);
            //Response.Write(contact.EmailAddress);
            Utility.CreateNewContact(AuthenticationData, contact);
        }
        else
        {
            throw new ConstantException(String.Format(System.Globalization.CultureInfo.CurrentCulture,
                                "Email address \"{0}\" is already a contact", txtEmail.Text.Trim()));
        }
    }
The GetContactInformation method:
private Contact GetContactInformation(string email, string name)
    {
        Contact contact = new Contact();        
        contact.EmailAddress = Server.HtmlEncode(email);
        contact.FirstName = Server.HtmlEncode(name);
        //contact.LastName = Server.HtmlEncode(txtName.Text.Trim());
        // Adding to List
        ContactOptInList contactOptInList = new ContactOptInList();
        contactOptInList.ContactList = new ContactList("Contacts from Contact Form", true);
        contact.ContactLists.Add(contactOptInList);
        return contact;
    }
I believe everything works up to the commented out code: Response.Write(contact.EmailAddress);
Everything is as it shows in the sample project as far as I can tell. Perhaps a set of fresh eyes could point some bug out to me? I would appreciate any assistance on this. Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Employee

Re: Creating a contact using a C#.Net solution.

Hi Kate,

 

You can retrieve all of your lists by performing a GET on https://api.constantcontact.com/ws/customers/{username}/lists. This will return up to 50 lists that are in your account, and a link to the next set of 50 if one exists. You can find more information on this at Retrieving a Contact List Collection.

 

As far as the C#.NET Library goes, this can be done by:

 

AuthenticationData authdata = new AuthenticationData();
authdata.Username = "USERNAME";
authdata.Password = "PASSWORD";
authdata.ApiKey = "APIKEY";
string nextLink;
IList<ContactList> lists = Utility.GetUserContactListCollection(authdata, out nextLink);

 

I hope that this helps. Please let us know if you have any other questions regarding this. Thanks!

David J

View solution in original post

11 REPLIES 11
Highlighted
Occasional Participant

Re: Creating a contact using a C#.Net solution.

Ok, so an update. I believe it has to do with not being able to get the exact ID of the list. Is there anyway to grab the ID of the lists in Constant Contact? I haven't been able to find a way.

 

I found this out by trying:

 

 

Response.Write(contactOptInList.ContactList.Name);
Response.Write(contactOptInList.ContactList.Id);

 

 

The first one prints out with the name I gave it, however, the ID does not print out at all, which makes me believe it isn't being set properly. 

 

 

Highlighted
Occasional Participant

Re: Creating a contact using a C#.Net solution.

So I have confirmed that the list is the issue. I tried adding a contact using this:

 

contactOptInList.ContactList = new ContactList("1");

 

And it was added to my default list. I need to know which ID my lists are so I can add them to the correct list. Is there anyway to find out the ID of all my lists?

Highlighted
Employee

Re: Creating a contact using a C#.Net solution.

Hi Kate,

 

You can retrieve all of your lists by performing a GET on https://api.constantcontact.com/ws/customers/{username}/lists. This will return up to 50 lists that are in your account, and a link to the next set of 50 if one exists. You can find more information on this at Retrieving a Contact List Collection.

 

As far as the C#.NET Library goes, this can be done by:

 

AuthenticationData authdata = new AuthenticationData();
authdata.Username = "USERNAME";
authdata.Password = "PASSWORD";
authdata.ApiKey = "APIKEY";
string nextLink;
IList<ContactList> lists = Utility.GetUserContactListCollection(authdata, out nextLink);

 

I hope that this helps. Please let us know if you have any other questions regarding this. Thanks!

David J

View solution in original post

Highlighted
Occasional Participant

Re: Creating a contact using a C#.Net solution.

Hi David,

 

     Thank you very much for replying. I appreciate it. I was able to use the C#.NET Library to display the list IDs so I now have this working correctly. Thank you for your assistance.

Highlighted
Occasional Visitor

Re: Creating a contact using a C#.Net solution.

Hi David,

 

What if we want to show more than 50 lists?

 

Thanks

Highlighted
Employee

Re: Creating a contact using a C#.Net solution.

Hi EytanD,

 

GetUserContactListCollection can be used to pull more than 50 lists as well utilizing the out variable and passing it back into the function.  

 

In the above example you used to get the first 50:

 

IList<ContactList> lists = Utility.GetUserContactListCollection(authdata, out nextChunk);

 nextChunk will contain the URI for Lists greater than 50.  To get those Lists:

 

IList<ContactList> lists = Utility.GetUserContactListCollection(authdata, out nextChunk);
            
IList<ContactList> lists2 = Utility.GetUserContactListCollection(authdata, nextChunk, out anotherNextChunk);

 

Help this helps,

Andrew Weber
Associate Product Manager for EventSpot
Highlighted
Moderator

Re: Creating a contact using a C#.Net solution.

To clarify a little on Andres post, nextChunk will be NULL if there are no additional lists to pull back. So, you can run a simple loop to pull all the lists down. Keep in mind, if you're talking 1000s of lists (however unlikely), this could take a few seconds and you may want to put in a progress bar or notification method in place to let the user know that you're still pulling new lists:

 

 

 

String nextChunk, currentChunk = string.Empty;
List<ContactList> lists = new List<ContactList>();
do {     lists.AddRange((List<ContactList>)Utility.GetUserContactListCollection(authdata, currentChunk, out nextChunk));     currentChunk = nextChunk; } while (nextChunk != string.Empty);
Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Visitor

Re: Creating a contact using a C#.Net solution.

Thanks Dave and Andrew :smileyhappy:.

 

One more question what if I want to add a contact by providing name of the list instead of List Id

like in the example below.

 

ContactOptInList contactOptInList = new ContactOptInList();

contactOptInList.ContactList = new ContactList("List Name", false);
contact.ContactLists.Add(contactOptInList);

Thanks.

Highlighted
Moderator

Re: Creating a contact using a C#.Net solution.

The constructor for ContactList which accepts a name and the OptInDefault value (true/false) is only intended to be used when creating a new list.  Whenever you need to use a list within a Contact action or a Campaign action, you will need to provide a ContactList object that has the ID set.  You can either permanently store the List ID on your side once you choose the list you want or you can continue to use the API to pull the list(s) in question on demand using the method above.

 

Unfortunately, there is no option for looking up a list by name currently.  We will update if this feature is released in the future.

Dave Berard
Senior Product Manager, 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