How To: Create A New Contact From Scratch Using ASP.NET API

Highlighted
Occasional Participant

How To: Create A New Contact From Scratch Using ASP.NET API

This shows how to add a new Contact.


Most of the code is from the API and examples, with some changes for easier use.


To Delete a contact, I personally have a "Deleted" list - So I remove him from all lists, and add him to that one :p


This is still pretty buggy, but I hope it helps someone :)


 


// 3 usings From the API


using ConstantContactBO;

using ConstantContactUtility;

using UploadContactForm.App_Code;


 


    private void addContact(string email, string name, string surname, string listToAdd)

    {

        string[] emailAddress = new string[] { email };

        string nextChunkId;

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

        if (list.Count == 0) // If the e-mail address doesn't exist

        {

            // Create new Contact

            Contact contact = GetContactInformation(email, name, surname, listToAdd);

            Utility.CreateNewContact(AuthenticationData, contact);

        }

        else

        {

            // Get his original details

            Contact c1 = Utility.GetContactDetailsById(AuthenticationData, list.Id);



            // Remove him from any list he was in

            Utility.RemoveContactFromAllLists(AuthenticationData, list.Id);



            // Update His Info

            c1.Name = name;

            c1.LastName = surname;



            // And re-add him

            ContactOptInList contactOptInList = new ContactOptInList();

            contactOptInList = new ContactOptInList();

            contactOptInList.ContactList = new ContactList(listToAdd);

            c1.ContactLists.Add(contactOptInList);

            Utility.UpdateContactSmallForm(AuthenticationData, c1);

        }

    }





    private AuthenticationData AuthenticationData

    {

        get

        {

            if (Session == null)

            {

                Session.Add("AuthenticationData", ConstantContact.AuthenticationData);

            }

            return (AuthenticationData)Session;

        }

    }





    private Contact GetContactInformation(string email, string name, string surname, string listToAdd)

    {

        Contact contact = new Contact();



        contact.EmailAddress = Server.HtmlEncode(email);

        contact.FirstName = Server.HtmlEncode(name);

        contact.LastName = Server.HtmlEncode(surname);



        ContactOptInList contactOptInList = new ContactOptInList();

        contactOptInList.ContactList = new ContactList(province);

        contact.ContactLists.Add(contactOptInList);



        return contact;

    }

Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured