uploading (importing) new contact list .net working example

Highlighted
Visitor

uploading (importing) new contact list .net working example

Hi 

I have downloaded the CTCT wrapper, complied it and started testing with it.

I did some very simple test like getting a list.

I am looking for a sample code C# or VB.net to upload contacts (for now just email address and names) into a new list

I would also like if possible to get sample code to update an existing list.

Thanks

Myalo

1 REPLY 1
Highlighted
Honored Contributor

Re: uploading (importing) new contact list .net working example

Hello,

 

If you are looking to add/update individual contacts, then you could implement your code in a manner as shown here:

            // In order to bulk import contacts, we will need a List of AddContactsImportData objects, a single AddContact object, and a Constant Contact object.
            // We also need two List<String> instances for our contact lists and import columns.
            IList<CTCT.Components.Activities.AddContactsImportData> contactsToAdd = new List<CTCT.Components.Activities.AddContactsImportData>();
            IList<String> lists = new List<String>(), columns = new List<String>();
            CTCT.Components.Activities.AddContacts importActvity;

            // To create an instance of the ConstantContact class, you will need an API key and Access Token. 
            // You can find information about how to obtain these in our Get Started guide here:
            // http://developer.constantcontact.com/get-started.html
            CTCT.ConstantContact _cc = new CTCT.ConstantContact("API_KEY", "ACCESS_TOKEN");
            CTCT.Components.Contacts.Contact contact;
            string emailAddress = "test@test.com";
            CTCT.Components.ResultSet<CTCT.Components.Contacts.Contact> results;
            CTCT.Components.Contacts.ContactList list;
            bool exists = false;

            // Search for a contact by email address
            results = _cc.GetContacts(emailAddress, null, null, null);

            // Check of the contact already exists. If it does then we will update the existing contact. If not we will create a new contact.
            if (results.Results.Count > 0)
            {
                contact = results.Results[0];
                exists = true;
            }
            else
            {
                contact = new CTCT.Components.Contacts.Contact();
                exists = false;
            }
            // Create a list object that can be attached to the contact and then add any details you want to the contact.
            list = new CTCT.Components.Contacts.ContactList();
            list.Id = "LIST_ID";
            contact.FirstName = "Test";
            contact.LastName = "Person";
            contact.Lists.Add(list);

            // When we are adding or updating the contact, the second parameter indicates if that action is initiated by the contact being updated.
            // If this parameter is set to true, then the contact will receive an email verification of the changes being made.
            // This logic will update an existing contact or create a new contact.
            if (exists)
            {
                _cc.UpdateContact(contact, false);
            }
            else
            {
                _cc.AddContact(contact, false);
            }

 

 

 

If you are looking to add/update multiple contacts at the same time, then you will want to use our Bulk Activity tools to complete this process in a batch and automatically add and updates. Here an an example of how to create a bulk activity for uploading contacts:

            // In order to bulk import contacts, we will need a List of AddContactsImportData objects, a single AddContact object, and a Constant Contact object.
            // We also need two List<String> instances for our contact lists and import columns.
            IList<CTCT.Components.Activities.AddContactsImportData> contactsToAdd = new List<CTCT.Components.Activities.AddContactsImportData>();
            IList<String> lists = new List<String>(), columns = new List<String>();
            CTCT.Components.Activities.AddContacts importActvity;

            // To create an instance of the ConstantContact class, you will need an API key and Access Token. 
            // You can find information about how to obtain these in our Get Started guide here:
            // http://developer.constantcontact.com/get-started.html
            CTCT.ConstantContact _cc = new CTCT.ConstantContact("API_KEY", "ACCESS_TOKEN");

            // This is a very simple loop that demonstrates how to add 100 identical contacts through a bulk activity
            // By using a loop to iterate through your own data, this could easily be used to create a list of unique contacts.
            for (var i = 1; i < 100; i++)
            {
                
                // First we create an instance of the AddContactsImportData class
                var currentContact = new CTCT.Components.Activities.AddContactsImportData();
                
                // Then we add and Email address, First Name, and Last Name
                currentContact.EmailAddresses.Add("email@example.com");
                currentContact.FirstName = "Example";
                currentContact.LastName = "Contact";
                
                // Then we add it to the List we created and continue the loop.
                contactsToAdd.Add(currentContact);
            }

            // Once the list of contacts is created, we can then create the activity.
            // We will start by assigning the contact list. If you do not know your List ID, it can be found by using the I/O Docs:
            // https://constantcontact.mashery.com/io-docs
            lists.Add("LIST_ID");
            
            // Next we will specify the columns that we want to import. A list of column names can be found here: 
            // http://developer.constantcontact.com/docs/bulk_activities_api/bulks-activities-import-contacts-api.html
            columns.Add("EMAIL");
            columns.Add("FIRST NAME");
            columns.Add("LAST NAME");
            
            // Next, we will create a new AddContacts instance using the information generated above.
            importActvity = new CTCT.Components.Activities.AddContacts(contactsToAdd, lists, columns);

            try {
                // And lastly, we will send the bulk activity to Constant Contact.
                _cc.CreateAddContactsActivity(importActvity);
            } catch (CTCT.Exceptions.CtctException ex) {
                // If there is an error, print the exception message to the console.
                System.Console.WriteLine(ex.Message);
            }

            // This will start the activity processing on Constant Contact. Most Bulk uploads will take between 3 and 10 minutes to complete. 
            // Very large uploads can sometimes take longer.

 

If you have any difficulty with these examples, please let me know!

 

Sincerely,

 

Elijah G.
API Support Engineer
Developer Portal

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