I will be showing you how to import and update a single contact from your third party system to Constant Contact using the Constant Contact API. Here is the API description for the POST (Create) call and PUT (Update) call.
[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.GetSingleContact("contact@company.com"); createContact(dt, lists); //third value is the ID from a specific contact, it is required to update a specific contact updateContact(dt, lists, "1239676744"); } private static void createContact(DataTable dt, List<string> lists) { if (dt.Rows.Count >= 1) { var contactService = _constantContactFactory.CreateContactService(); Address address = new Address() { Line1 = toString(dt.Rows[0]["address"]), City = toString(dt.Rows[0]["city"]), AddressType = "PERSONAL", StateCode = toString(dt.Rows[0]["state"]), PostalCode = toString(dt.Rows[0]["zip"]) }; List<ContactList> contactLists = new List<ContactList> (); foreach(string list in lists) { ContactList contactList = new ContactList() { Id = list }; contactLists.Add(contactList); } CustomField cfield = new CustomField() { Name = "custom_field_1", Value = toString(dt.Rows[0]["schoolname"]) }; Contact contact = new Contact() { EmailAddresses = new List<EmailAddress> { new EmailAddress(toString(dt.Rows[0]["primaryemail"])) }, FirstName = toString(dt.Rows[0]["firstname"]), LastName = toString(dt.Rows[0]["lastname"]), JobTitle = toString(dt.Rows[0]["job_role_name"]), CompanyName = toString(dt.Rows[0]["districtname"]), HomePhone = toString(dt.Rows[0]["primaryphone"]), Addresses = new List<Address> { address }, Status = "ACTIVE", Lists = contactLists, CustomFields = new List<CustomField> { cfield } }; contactService.AddContact(contact, false); } } private static void updateContact(DataTable dt, List<string> lists, string id) { if (dt.Rows.Count >= 1) { var contactService = _constantContactFactory.CreateContactService(); Address address = new Address() { Line1 = toString(dt.Rows[0]["address"]), City = toString(dt.Rows[0]["city"]), AddressType = "PERSONAL", StateCode = toString(dt.Rows[0]["state"]), PostalCode = toString(dt.Rows[0]["zip"]) }; List<ContactList> contactLists = new List<ContactList> (); foreach(string list in lists) { ContactList contactList = new ContactList() { Id = list }; contactLists.Add(contactList); } CustomField cfield = new CustomField() { Name = "custom_field_1", Value = toString(dt.Rows[0]["schoolname"]) }; Contact contact = new Contact() { Id = id, EmailAddresses = new Lis < EmailAddress > { new EmailAddress(toString(dt.Rows[0]["primaryemail"])) }, FirstName = toString(dt.Rows[0]["firstname"]), LastName = toString(dt.Rows[0]["lastname"]), JobTitle = toString(dt.Rows[0]["job_role_name"]), CompanyName = toString(dt.Rows[0]["districtname"]), HomePhone = toString(dt.Rows[0]["primaryphone"]), Addresses = new List<Address> { address }, Status = "ACTIVE", Lists = contactLists, CustomFields = new List<CustomField> { cfield } }; contactService.UpdateContact(contact, false); } } #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.GetSingleContact("contact@company.com") createContact(dt, lists) 'third value is the ID from a specific contact, it is required to update a specific contact updateContact(dt, lists, "1239676744") End Sub Private Shared Sub createContact(dt As DataTable, lists As List(Of String)) If dt.Rows.Count >= 1 Then Dim contactService = _constantContactFactory.CreateContactService() Dim address As New Address() With { _ Key .Line1 = toString(dt.Rows(0)("address")), _ Key .City = toString(dt.Rows(0)("city")), _ Key .AddressType = "PERSONAL", _ Key .StateCode = toString(dt.Rows(0)("state")), _ Key .PostalCode = toString(dt.Rows(0)("zip")) _ } Dim contactLists As New List(Of ContactList)() For Each list As String In lists Dim contactList As New ContactList() With { _ Key .Id = list _ } contactLists.Add(contactList) Next Dim cfield As New CustomField() With { _ Key .Name = "custom_field_1", _ Key .Value = toString(dt.Rows(0)("schoolname")) _ } Dim contact As New Contact() With { _ Key .EmailAddresses = New List(Of EmailAddress)() From { _ New EmailAddress(toString(dt.Rows(0)("primaryemail"))) _ }, _ Key .FirstName = toString(dt.Rows(0)("firstname")), _ Key .LastName = toString(dt.Rows(0)("lastname")), _ Key .JobTitle = toString(dt.Rows(0)("job_role_name")), _ Key .CompanyName = toString(dt.Rows(0)("districtname")), _ Key .HomePhone = toString(dt.Rows(0)("primaryphone")), _ Key .Addresses = New List(Of Address)() From { _ address _ }, _ Key .Status = "ACTIVE", _ Key .Lists = contactLists, _ Key .CustomFields = New List(Of CustomField)() From { _ cfield _ } _ } contactService.AddContact(contact, False) End If End Sub Private Shared Sub updateContact(dt As DataTable, lists As List(Of String), id As String) If dt.Rows.Count >= 1 Then Dim contactService = _constantContactFactory.CreateContactService() Dim address As New Address() With { _ Key .Line1 = toString(dt.Rows(0)("address")), _ Key .City = toString(dt.Rows(0)("city")), _ Key .AddressType = "PERSONAL", _ Key .StateCode = toString(dt.Rows(0)("state")), _ Key .PostalCode = toString(dt.Rows(0)("zip")) _ } Dim contactLists As New List(Of ContactList)() For Each list As String In lists Dim contactList As New ContactList() With { _ Key .Id = list _ } contactLists.Add(contactList) Next Dim cfield As New CustomField() With { _ Key .Name = "custom_field_1", _ Key .Value = toString(dt.Rows(0)("schoolname")) _ } Dim contact As New Contact() With { _ Key .Id = id, _ Key .EmailAddresses = New List(Of EmailAddress)() From { _ New EmailAddress(toString(dt.Rows(0)("primaryemail"))) _ }, _ Key .FirstName = toString(dt.Rows(0)("firstname")), _ Key .LastName = toString(dt.Rows(0)("lastname")), _ Key .JobTitle = toString(dt.Rows(0)("job_role_name")), _ Key .CompanyName = toString(dt.Rows(0)("districtname")), _ Key .HomePhone = toString(dt.Rows(0)("primaryphone")), _ Key .Addresses = New List(Of Address)() From { _ address _ }, _ Key .Status = "ACTIVE", _ Key .Lists = contactLists, _ Key .CustomFields = New List(Of CustomField)() From { _ cfield _ } _ } contactService.UpdateContact(contact, False) End If 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
[bottomads][/bottomads]
RT @MusicHackFest: Import/Update #Contact using #ConstantContact #Email #Marketing #API in #ASPNET #CSharp #VBNET) http://t.co/rjlxkHfWVI
Import/Update #Contact using #ConstantContact #Email #Marketing #API in #ASPNET #CSharp #VBNET) http://t.co/rjlxkHfWVI
RT MusicHackFest: Import/Update #Contact using #ConstantContact #Email #Marketing #API in #ASPNET #CSharp #VBNET) http://t.co/rjlxkHfWVI
Excellent blog post , I Appreciate the analysis ! Does anyone know if my business could get ahold of a blank 2002 CAA Form 2.7 document to complete ?
Do you have a complete vb.net project I could get or download
I did the project in C# and just translated that portion in an online C# to VB.NET translator, sorry.