Archive for May, 2009

WCF REST Starter Kit to Constant Contact Sample Application

I’ve had a few requests to get more details on how I used the WCF REST Starter Kit to integrate with the Constant Contact AtomPub API (posted here). It’s shamefully simple but here it is. If you are not satisfied I will give you your money back.

Requires:

Download Sample App

Notes:

  • Update the app.config with your account settings from Constant Contact.
  • Sample is using Http Digest for authentication. If there is interest I can update for OAuth.
using System;
using System.Configuration;
using System.Collections.Generic;
using Freemind.Domain.Email;

namespace Freemind.ConstantContactSample
{
   class Program
   {
      static void Main(string[] args)
      {
         try
         {
            Console.WriteLine("Begin loading Contact Lists");

            // Connect and list Contact Lists
            ContactListProvider provider = new ContactListProvider(
               ConfigurationManager.AppSettings["ConstantContactAccountName"],
               ConfigurationManager.AppSettings["ConstantContactPassword"]);

            List lists = provider.GetContactLists();

            if (lists != null)
            {
               foreach (ContactList list in lists)
               {
                  Console.WriteLine("  list:{0} id:{1}", list.Name, list.id);
               }
            }

            Console.WriteLine("Completed loading Contact Lists");
            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
         }
         catch (Exception ex)
         {
            Console.WriteLine("Error: {0}",ex.Message);
         }
      }
   }
}

Using WCF REST Starter Kit to Access Constant Contact API

Problem: Allow customers to sign up for email newsletters and synchronize those accounts with Constant Contact.

Constant Contact is a company that provides Email Marketing services. You can use their editor to create nice looking emails and then configure a list of contacts. Constant Contact handles all the email sending, bounces, and unsubscribe activities. They also provide excellent reporting features to track how many people actually view your emails or click on the contained links.

The nice folks at Constant Contact provide a set of web services for accessing account data. These interfaces are implemented as REST services utilizing the Atom Publication Protocol (AtomPub) and Atom Syndication Format. At first it might seem odd to be accessing data objects via a blogging protocol but it actually works quite naturally. So well that Google’s GData and Microsoft’s ADO.NET Data Services also support this technique.

Writing all the HTTP, Auth, and XML parsing code can be a real drag. Luckily Microsoft is releasing a WCF REST Starter Kit. From the CodePlex summary:

The WCF REST Starter Kit is a set of .Net Framework classes and Visual Studio features and templates that enable users to create and access REST-style Windows Communication Foundation (WCF) services. These services are based on the WCF web programming model available in .Net 3.5 SP1. The starter kit also contains the full source code for all features, detailed code samples, and unit tests.

This starter kit makes accessing the Constant Contact API quite simple. Call GetEntry or GetFeed with a URI and get back a SyndicationItem or SyndicationFeed (collection of items). Similar methods exist for adding and updating data (AddEntry and UpdateEntry).

The following is one method from my ContactListProvider class:

public ContactList GetContactList(string id)
{
   AtomPubClient client = new AtomPubClient();
   client.TransportSettings.Credentials = GetLoginCredentials();

   // Place in a try block to ensure that any errors are caught
   try
   {
      SyndicationItem item = client.GetEntry(new Uri(id));

      ContactList list = new ContactList(item.Content as XmlSyndicationContent);

      return list;
   }
   catch (WebException ex)
   {
      _log.Error("WebException: " + ex.Status + " " + ex.Message, ex);
      return null;
   }
   catch (Exception ex)
   {
      // Get the exception type
      _log.Error("Exception: " + ex.Message, ex);
      return null;
   }
}

Contact me via comments if you want a copy of all the providers.