cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the content of the campaign

Occasional Contributor

Getting the content of the campaign

Seems nothing I do in this API gets me the actual email html. What can I do to pull the email content onto a page in my site. Archive purposes. I've got the listing no problem.

2 REPLIES 2
Occasional Contributor

Re: Getting the content of the campaign

Using C# examples.

CTCT Employee

Re: Getting the content of the campaign

Hy Tyler,

 

In order to grab the the HTML from an email using our API it would have to be a custom coded email.  I created an example for you in C# so you can see how to grab the data.  If the email was created with the visual editor, then it wouldn't return anything.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace WebApplication4
{
    public partial class _Default : System.Web.UI.Page
    {
        string xmlResponse = "";
        string apiKey = "your-api-key";
        string username = "your-user-name";
        string password = "your-password";
        string name = "";
        const string EmailElementName = "EmailContent";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void getData_btn_Click(object sender, EventArgs e)
        {
            xmlResponse_lbl.Text = getRequest();
        }

        private string getRequest()
        {
            // Remeber to add using System.net
            // Create login creditials
            CredentialCache LoginCredentials = new CredentialCache();

            //Add a new credential for this account
            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/{username}"),
                "Basic", // Authentication Type
                new NetworkCredential(apiKey + '%' + username, password));

            // Create the request, you can not new.  All requests must be created with WebRequest.Create
            // Use our URI that we want to get
            WebRequest Request = WebRequest.Create("https://api.constantcontact.com/ws/customers/{username}/campaigns/{CampaignID}");

            // Set Request credentials
            Request.Credentials = LoginCredentials;

            // Place in a try block to ensure that any errors are caught
            try
            {
                //Get resposne from server
                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

                //Get our Data
                StreamReader reader = new StreamReader(Response.GetResponseStream());
                XmlTextReader xmlreader = new XmlTextReader(reader);
                XPathDocument doc = new XPathDocument(xmlreader);

                // initialize navigator
                XPathNavigator pn = doc.CreateNavigator();
                string ConstantNamespace = @"http://ws.constantcontact.com/ns/1.0/";
                string AtomNamespace = @"http://www.w3.org/2005/Atom";
                // initialize namespace manager
                XmlNamespaceManager resolver = new XmlNamespaceManager(pn.NameTable);
                resolver.AddNamespace("at", AtomNamespace);
                resolver.AddNamespace("cc", ConstantNamespace);
                const string xpathSelect = @"//cc:Campaign";
                XPathExpression expr = pn.Compile(xpathSelect);
                expr.SetContext(resolver);

                XPathNodeIterator nodes = pn.Select(expr);
                while (nodes.MoveNext())
                {
                    // save current node
                    XPathNavigator node = nodes.Current;

                    if (node.HasChildren)
                    {
                        node.MoveToFirstChild();

                        do
                        {
                            switch (node.Name)
                            {
                                case EmailElementName:
                                    name = node.Value;
                                    break;

                            }// End Switch
                        } while (node.MoveToNext());
                    } // End do While

                } // End while nodes.MoveNext

                reader.Close();
                xmlreader.Close();

                return name;



            }

            catch (WebException e)
            {
                // Web Exception
            }
            
            catch (Exception e)
            {
                // Exception
            }

            // Parse the Data with XPATH
            return name;
        }
    }
}

 Please let me know if I can be of any more assistance. 

 

Regards,

Ryan Davis
Quality Engineer