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.

not able to add contact (C#)

Highlighted
Participant

not able to add contact (C#)

 I am not able to add a contact to my list. Am I doing anything wrong here. This code lives on a script task in a SSIS package. When this script task is executed, the Main method is called. When i run the package, this task is run without any errors. But when i check the contacts in constant contact, they are not there. Please help!!


My target is the 3rd list. 



public void Main()


        {


            string result = Bulk_POST("key", "myusername", "mypassword");


            Dts.TaskResult = (int)ScriptResults.Success;


        }


        public string Bulk_POST(string APIKey, string uName, string pWord)


        {


            CredentialCache LoginCredentials = new CredentialCache();


            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +


                uName + "/activities");


            request.Credentials = LoginCredentials;


            request.Method = "POST";


            request.ContentType = "application/atom+xml";


            sListUri = "https://api.constantcontact.com/ws/customers/" + uName + "/lists/3";


            StringBuilder data = new StringBuilder();


            data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8));


            data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,First Name,Last Name,Company" + "\n"), Encoding.UTF8));


            data.Append(HttpUtility.UrlEncode(("test@gmail.com" + ","  + "Test" + "," + "Test" + "," + "testcompany"), Encoding.UTF8));


            data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));


            byte[] byteArray = Encoding.UTF8.GetBytes(data.ToString());


            try


            {


                request.ContentLength = byteArray.Length;


                string XMLResponse = "Bytes to send: " + byteArray.Length;


                Stream streamRequest = request.GetRequestStream();


                streamRequest.Write(byteArray, 0, byteArray.Length);


                streamRequest.Close();


                HttpWebResponse Response = (HttpWebResponse)request.GetResponse();


                StreamReader Reader = new StreamReader(Response.GetResponseStream());


                XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();


                Reader.Close();


                Response.Close();


                return XMLResponse;


            }


            catch (WebException e)


            {


                return ("WebException: " + e.Status + "With response: " + e.Message);


            }


 


            catch (Exception e)


            {


                return ("Exception: " + e.Message);


            }


        }


 The Bladerunner

5 REPLIES 5
Highlighted
Employee

Re: not able to add contact (C#)

I see a few things here that I needed to adjust in order for this to successfully create a bulk activity in my account.


 


1. The Column for "Company" needs to be changed to "Company Name", you can find a full list of column names at our Bulk Activity reference.


2. The content-type for this type of bulk activity would be 'application/x-www-form-urlencoded' as opposed to 'application/atom+xml'.


 


I think if you make these minor adjustments your bulk contact upload activity will post successfully. I hope this helps, let me know if you have any other questions or issues regarding this.

David J

Highlighted
Participant

Thank You

Thank You very much on pointing my mistakes. That worked like a charm. Thanks once again.  

 The Bladerunner

Highlighted
Participant

targetinvocation exception

 Hi David,


This was working fine few days ago. I added about 50-60 contacts in my list. I tried this 4-5 times and it worked. But today when i tried to run this, I was getting an exception. FYI - I have been using trial version (60 days free) account on constant and contact. Not sure if that would have caused the problem. Here is my code.


 


public void Bulk_POST(string APIKey, string uName, string pWord)


        {


            SqlConnection scon = new SqlConnection(@"connection string goes here");


            SqlCommand scmd = new SqlCommand("SELECT * FROM Table WHERE LEN() > 0", scon);


            SqlDataReader sdr;


            try


            {


                scon.Open();


                sdr = scmd.ExecuteReader();


                while (sdr.Read())


                {


                    CredentialCache LoginCredentials = new CredentialCache();


                    LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));


                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +


                        uName + "/activities");


                    request.Credentials = LoginCredentials;


                    request.Method = "POST";


                    request.ContentType = "application/x-www-form-urlencoded";


                    sListUri = "https://api.constantcontact.com/ws/customers/" + uName + "/lists/3";


                    StringBuilder data = new StringBuilder();


                    data.Append("activityType=" + HttpUtility.UrlEncode("SV_ADD", Encoding.UTF8));


                    data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,First Name,Last Name,Company Name,CUSTOM FIELD 1" + "\n"), Encoding.UTF8));


                    data.Append(HttpUtility.UrlEncode((sdr.ToString() + "," + sdr.ToString() + "," + sdr.ToString() + "," + "SA" + "," + sdr.ToString()), Encoding.UTF8));


                    data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));


                    byte[] byteArray = Encoding.UTF8.GetBytes(data.ToString());


                    try


                    {


                        request.ContentLength = byteArray.Length;


                        string XMLResponse = "Bytes to send: " + byteArray.Length;


                        Stream streamRequest = request.GetRequestStream();


                        streamRequest.Write(byteArray, 0, byteArray.Length);


                        streamRequest.Close();


                        HttpWebResponse Response = (HttpWebResponse)request.GetResponse();


                        //THE EXCEPTION IS THROWN IN THE LINE BELOW.


                        StreamReader Reader = new StreamReader(Response.GetResponseStream());


                        XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();


                        Reader.Close();


                        Response.Close();


                        //return XMLResponse;


                    }


                    catch (WebException e)


                    {


                        throw new WebException ("WebException: " + e.Status + "With response: " + e.Message);


                    }


 


                    catch (Exception e)


                    {


                        throw new Exception ("Exception: " + e.Message);


                    }


                }


                sdr.Close();


             }


             catch (Exception ex)


            {


                throw new Exception("Error occured: " + ex);


            }


            finally


            {


               scon.Close();


            }


        }


This same code was working fine like couple days ago. Please let me know if you want any further information.


Thanks!


 

 The Bladerunner

Highlighted
Employee

API not working

Hello,


Yesterday our API servers were temporarily down for a period between 12:50pm and 6:00pm. During that time you would have received errors such as that.


It should be working fine now, if you are still running into issues after this, please let us know here.


Regards,

Benjamin Soder
NOC Analyst
Constant Contact
Highlighted
Participant

Yup its working

That was the problem then. I ran it this morning without any problem. Thanks David. 

 The Bladerunner

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