In this post I will show you how to use the Bulk Activities from the Constant Contact API to import contacts from your custom application to your Constant Contact Account.
[topads][/topads]
First of all, I should note that I created this as a Console Application, however the same concept should work for a web app.
You need to have your API Key and Access Token in handy, you will need them to connect to the Constant Contact API. Once you have them I would suggest to add them to your web.config or app.config, this way you don’t have to hard code them in your app.
In the code snippet below I am getting my data from a Database Access Layer class and passing it to the methods in a DataTable. The rest of the code should be pretty self explanatory.
C# Code
namespace ConstantContact { class Program { private static string _apiKey = ConfigurationManager.AppSettings["APIKey"].ToString(); private static string _accessToken = ConfigurationManager.AppSettings["AccessToken"].ToString(); private static DatabaseAccessLayer _dal = new DatabaseAccessLayer(); private static IUserServiceContext _userServiceContext = new UserServiceContext(_accessToken, _apiKey); private static ConstantContactFactory _constantContactFactory = new ConstantContactFactory(_userServiceContext); static void Main(string[] args) { List<string> lists = new List<string>() { "1479986508" }; DataTable dt = _dal.GetAllContacts(); bulkCreateUpdate(dt, lists); } private static void bulkCreateUpdate(DataTable dt, List<string> lists) { List<AddContactsImportData > bulkContacts = new List<AddContactsImportData>(); var activityService = _constantContactFactory.CreateActivityService(); foreach(DataRow row in dt.Rows) { Address address = new Address() { Line1 = toString(row["address"]), City = toString(row["city"]), AddressType = "PERSONAL", StateCode = toString(row["state"]), PostalCode = toString(row["zip"]) }; CustomField cfield = new CustomField() { Name = "CUSTOM FIELD 1", Value = toString(row["schoolname"]) }; AddContactsImportData singleContact = new AddContactsImportData() { EmailAddresses = new List<string> { toString(row["primaryemail"]) }, FirstName = toString(row["firstname"]), LastName = toString(row["lastname"]), BirthdayDay = toString(row["dob"]).Split('-')[1], BirthdayMonth = toString(row["dob"]).Split('-')[0], JobTitle = toString(row["job_role_name"]), CompanyName = toString(row["districtname"]), HomePhone = toString(row["primaryphone"]), Addresses = new List<Address> { address }, CustomFields = new List<CustomField> { cfield } }; bulkContacts.Add(singleContact); } List<string > columnNames = new List<string>() { "EMAIL", "FIRST NAME", "LAST NAME", "BIRTHDAY_DAY", "BIRTHDAY_MONTH", "JOB TITLE", "COMPANY NAME", "HOME PHONE", "ADDRESS LINE 1", "CITY", "STATE", "ZIP/POSTAL CODE", "CUSTOM FIELD 1" }; var addContacts = new AddContacts(bulkContacts, lists, columnNames); var activity = activityService.CreateAddContactsActivity(addContacts); } #region Helper Methods private static string toString(Object obj) { return Convert.ToString(obj).Trim(); }#endregion } }
VB.NET Code
Namespace ConstantContact Class Program Private Shared _apiKey As String = ConfigurationManager.AppSettings("APIKey").ToString() Private Shared _accessToken As String = ConfigurationManager.AppSettings("AccessToken").ToString() Private Shared _dal As New DatabaseAccessLayer() Private Shared _userServiceContext As IUserServiceContext = New UserServiceContext(_accessToken, _apiKey) Private Shared _constantContactFactory As New ConstantContactFactory(_userServiceContext) Private Shared Sub Main(args As String()) Dim lists As New List(Of String)() From { _ "1479986508" _ } Dim dt As DataTable = _dal.GetAllContacts() bulkCreateUpdate(dt, lists) End Sub Private Shared Sub bulkCreateUpdate(dt As DataTable, lists As List(Of String)) Dim bulkContacts As New List(Of AddContactsImportData)() Dim activityService = _constantContactFactory.CreateActivityService() For Each row As DataRow In dt.Rows Dim address As New Address() With { _ Key .Line1 = toString(row("address")), _ Key .City = toString(row("city")), _ Key .AddressType = "PERSONAL", _ Key .StateCode = toString(row("state")), _ Key .PostalCode = toString(row("zip")) _ } Dim cfield As New CustomField() With { _ Key .Name = "CUSTOM FIELD 1", _ Key .Value = toString(row("schoolname")) _ } Dim singleContact As New AddContactsImportData() With { _ Key .EmailAddresses = New List(Of String)() From { _ toString(row("primaryemail")) _ }, _ Key .FirstName = toString(row("firstname")), _ Key .LastName = toString(row("lastname")), _ Key .BirthdayDay = toString(row("dob")).Split("-"C)(1), _ Key .BirthdayMonth = toString(row("dob")).Split("-"C)(0), _ Key .JobTitle = toString(row("job_role_name")), _ Key .CompanyName = toString(row("districtname")), _ Key .HomePhone = toString(row("primaryphone")), _ Key .Addresses = New List(Of Address)() From { _ address _ }, _ Key .CustomFields = New List(Of CustomField)() From { _ cfield _ } _ } bulkContacts.Add(singleContact) Next Dim columnNames As New List(Of String)() From { _ "EMAIL", _ "FIRST NAME", _ "LAST NAME", _ "BIRTHDAY_DAY", _ "BIRTHDAY_MONTH", _ "JOB TITLE", _ "COMPANY NAME", _ "HOME PHONE", _ "ADDRESS LINE 1", _ "CITY", _ "STATE", _ "ZIP/POSTAL CODE", _ "CUSTOM FIELD 1" _ } Dim addContacts = New AddContacts(bulkContacts, lists, columnNames) Dim activity = activityService.CreateAddContactsActivity(addContacts) End Sub #Region "Helper Methods" Private Shared Overloads Function toString(obj As [Object]) As String Return Convert.ToString(obj).Trim() End Function #End Region End Class End Namespace
If you do not have custom fields you do not have to create the columnNames list and just pass a null to the AddContacts method.
Bulk Activities documentation in Constant Contact.
[bottomads][/bottomads]
#Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http://t.co/teecAi29D3
RT @MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http://t.co/fPAHz…
RT @JGeZau: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http://t.co/teecAi29D3
#Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http://t.co/fPAHzgi4mm
RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #… http://t.co/fPAHzgi4mm
RT JGeZau: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http://t.co/fPAHzgi4mm
RT @MusicHackFest: RT JGeZau: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #VBNET) http:/…
RT @MusicHackFest: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #… http…
RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #… http://t.co/lrVPqrJFsT
RT @AspnetDaily: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNET (#CSharp and #… http…
RT AspnetDaily: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNE… http://t.co/fPAHzgi4mm
RT @MusicHackFest: RT AspnetDaily: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Marketing #API in #ASPNE… http…
RT MusicHackFest: RT AspnetDaily: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Market… http://t.co/fPAHzgi4mm
RT @MusicHackFest: RT MusicHackFest: RT AspnetDaily: RT MusicHackFest: #Import #Contacts in #Bulk using #ConstantContact Email Market… http…