Getting Started with the API

Frequent Participant

Getting Started with the API

Welcome to the Getting Started with the API Forum!

This forum is intended to help new and returning users of the Constant Contact APIs apply the API. Appropriate forum topics include " Getting Started" topics as well as "How Do I...?" postings.




To review existing topics or to start a new thread in this Getting Started forum, go here.
For a list of forums, go here.


Tom M
Group Product Manager – Content Editing
Constant Contact
11 REPLIES 11

Hi,

Can someone at CC upload sample JAVA Docs?

Thank you,

Diego
Frequent Participant

As this is a web services based API invoked via HTTP there is no direct access to the Constant Contact libraries and no JavaDoc. There is documented sample Java code showing some usage of the API here .
Tom M
Group Product Manager – Content Editing
Constant Contact
Participant

...or something else?

Javadoc for the sample app sounds like a good idea. We'll see what we can do.

Jim
Regular Participant

Anybody attempting to call any of the CC api's from c#? Any success stories out there?
Regular Participant

Have you had any success, anything to share with c#, we use vb.net but can easly convert.

Thank you,


Charles

Regular Participant

It's not pretty, but here it is. Thanks to Kevin Mandeville @ CC or this would have never worked. And the only reason I'm posting this is BECAUSE he helped me. I personally think the docs and the API for CC need a tremendous amount of work. There are three main functions to know. Calling url, posting a named param list, posting an atom entry. Here are some examples. Note they aren't complete because of cut and paste but you should have everything you need. This is posting a named param set, ie, adding email address in. This is how to retrieve a list


        public string retrieveLists(string accountname, string xpathselect)

        {

            string username = "asdf1234" + % + "uname";

            string completeurl = surl + accountname + @"/lists";



            List<string></string> al = new List<string></string>();



            if (xpathselect == String.Empty)

            {

                xpathselect = "//at:title|//at:id";

            }



            Uri address = new Uri(completeurl);

            Debug.WriteLine(completeurl);



            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Credentials = new NetworkCredential(username, pword);

            string st = String.Empty;



            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

            {                



                // Get the response stream  

                StreamReader reader = new StreamReader(response.GetResponseStream());



                XmlTextReader xmlreader = new XmlTextReader(reader);



                XPathDocument doc = new XPathDocument(xmlreader);

                XPathNavigator pn = doc.CreateNavigator();



                XmlNamespaceManager ns = new XmlNamespaceManager(pn.NameTable);

                ns.AddNamespace("at", "http://www.w3.org/2005/Atom");

                ns.AddNamespace("cc", "http://ws.constantcontact.com/ns/1.0");



                XPathNodeIterator nodes = pn.Select(xpathselect,ns);



                bool bfirst = true;

                string stemp = String.Empty;



                while (nodes.MoveNext())                

                {

                   

                    // note. Don't attemp to use attributes. There aren't any....



                    XPathNavigator node = nodes.Current;



                    if (bfirst)

                    {

                        stemp = node.Value;

                        bfirst = false;

                    }

                    else

                    {

                        al.Add(stemp + "|" + node.Value);

                        stemp = String.Empty;

                        bfirst = true;

                    }

                }

               

            }



            return al.ToArray();

        }

       

    //This is how to post an atom entry. Notice the content type <code>



  public string addNewList(string accountname, string listname)

        {



            string username = "asdf1234" + % + "uname";

            string completeurl = surl + accountname + @"/lists"; // collection uri

           

            Uri address = new Uri(completeurl);

            Debug.WriteLine(completeurl);

            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Credentials = new NetworkCredential(username, pword);

            request.Method = "POST";

            request.ContentType = @"application/atom+xml"; // key line



 StringBuilder data = new StringBuilder();

            data.Append(@"<entry xmlns="" www.w3.org=""></entry>");

            data.Append(@"<id></id>data:,");

            data.Append(@"");

            data.Append(@"<author></author>");

            data.Append(@"<updated></updated>2008-06-30");

            data.Append(@"<content application="" type=""></content>");

            data.Append(@"<contactlist xmlns="" ns="" ws.constantcontact.com=""></contactlist>");

            data.Append(@"<optindefault></optindefault>false");

            data.Append(@"<name></name>" + listname + "");

            data.Append(@"<sortorder></sortorder>92");

            data.Append(@"");

            data.Append(@"");

            data.Append(@"");



 byte byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());



            string st = String.Empty;



            // Set the content length in the request headers  

            request.ContentLength = byteData.Length;



            // Write data  

            using (Stream postStream = request.GetRequestStream())

            {

                postStream.Write(byteData, 0, byteData.Length);

            }



            // Get response  

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

            {                

                // Get the response stream  

                StreamReader reader = new StreamReader(response.GetResponseStream());

                st = reader.ReadToEnd();

            }



            return st;

        }

****email me at cbaugh@gmail.com for a clean copy of this post..


string username = "asdf1234" + % + "uname";

string completeurl = surl + accountname + @"/activities";            



            Uri address = new Uri(completeurl);

            Debug.WriteLine(completeurl);

            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Credentials = new NetworkCredential(username, pword);

            request.Method = "POST";

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



            StringBuilder data = new StringBuilder();

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

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

            data.Append("&amp;lists=" + HttpUtility.UrlEncode(listurl)); //  + @"lists/1"));

            byte byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());



            string st = String.Empty;



            // Set the content length in the request headers  

            request.ContentLength = byteData.Length;



            // Write data  

            using (Stream postStream = request.GetRequestStream())

            {

                postStream.Write(byteData, 0, byteData.Length);

            }



            // Get response  

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

            {                

                // Get the response stream  

                StreamReader reader = new StreamReader(response.GetResponseStream());

                st = reader.ReadToEnd();

            }



            return st;

        }

 
Frequent Participant

Interested in VB.Net? One user posted an example (Thanks!) here:
http://www.codescene.com/2008/09/using-the-constant-contact-api.php
Tom M
Group Product Manager – Content Editing
Constant Contact
Regular Participant

When attempting to use the second namespace in the XmlNamespaceManager (i.e. "cc") please add an additional slash at the end of the url. Without it, it will fail, but with it

//cc:ContactList/cc:Name

will work.
Regular Participant

please tell me how to retrieve contacts under a contact list
Regular Participant

I think the addNewList(string accountname, string listname) is to add a new contact list to an account. Please correct me if i am wrong.
But I couldn't find any place where we give the second parameter listname in the body of the function.

more over that I am getting 404 error when i run this programme.
please help.
Anonymous
Not applicable

Please note that the above example adds only one contact using Activities resource, but the Activities resource is only recommended for adding more than 25 contacts.  Please refer the documentation on the Activities resource to see how you can add more contacts to the example code above.


To add one contact, please use the Contacts resource.

Developer Portal

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

Visit Page