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.

Adding a contact to a list

Highlighted
Advisor

Adding a contact to a list

So I am an entry level developer and I have been assigned to a project using CC.  Right now I am creating a prototype with windows forms.

 

this is my code.

 

 

using

System;

using

System.Collections.Generic;

using

System.ComponentModel;

using

System.Data;

using

System.Drawing;

using

System.Linq;

using

System.Text;

using

System.Windows.Forms;

using

System.Net;

using

System.Xml;

using

System.Xml.Linq;

using

System.IO;

using

System.Xml.XPath;

 

namespace

WindowsFormsApplication1

{

publicpartialclassConstantContactForm : Form

    {

XNamespace Atom = "http://www.w3.org/2005/Atom";

       

XNamespace ConstantContact = "http://ws.constantcontact.com/ns/1.0/";

 

       

conststring API_USERNAME = "smimarketing";

       

conststring API_PASSWORD = "3tr6tegic";

       

conststring API_KEY = "f792af13-3f65-480e-a23f-80c97e476e22";

       

conststring API_RESOURCE_CONTACTS = "contacts";

       

       

conststring API_RESOURCE_LISTS = "lists";

       

conststring API_RESOURCE_CAMPAIGNS = "campaigns";

 

       

public ConstantContactForm()

        {

            InitializeComponent();

        }

       

privatevoid btn_Submit_Click(object sender, EventArgs e)

        {

           

string NewListName = txtbox_ListName.Text;

           

string Resource = API_RESOURCE_LISTS;

           

string Url = String.Format("https://api.constantcontact.com/ws/customers/{0}/{1}", API_USERNAME, Resource);

 

           

CredentialCache CredentialCache = newCredentialCache();

            CredentialCache.Add(

newUri(Url), "Basic", newNetworkCredential(API_KEY + "%" + API_USERNAME, API_PASSWORD));

           

try

            {

               

XDocument NewList = newXDocument


              {

               

newXDeclaration("1.0", "utf-8", "no"),

               

newXElement


                (

                    Atom +

"entry",

                   

newXAttribute("xmlns",

                       

"http://www.w3.org/2005/Atom"),

                   

newXElement(Atom + "id", "data:,"),

                   

newXElement


                    (

                    Atom +

"title", ""


                    ),

                   

newXElement(Atom + "author", ""),

                   

newXElement(Atom + "updated", "2011-09-02"),

                   

newXElement


                    (

                    Atom +

"content",

                   

newXAttribute("type", "application/vnd.ctct+xml"),

                   

newXElement


                    (

                        ConstantContact +

"ContactList",

                       

newXAttribute("xmlns",

                           

"http://ws.constantcontact.com/ns/1.0/"),

                       

newXElement(ConstantContact + "OptInDefault", "false"),

                       

newXElement(ConstantContact + "Name", NewListName),

                       

newXElement(ConstantContact + "SortOrder", "0")

                        )

                        )

                     )

                     );

               

string Response = NewList.Post(Url, CredentialCache);

               

MessageBox.Show("You have successfully created a new list!");

                txtbox_ListName.Clear();

            }

           

catch


            {

               

MessageBox.Show("An Error has occured!");

            }

        }

       

privatevoid btn_Display_Click(object sender, EventArgs e)

        {

            ShowResults();

        }

 

      

4 REPLIES 4
Highlighted
Advisor

Re: Adding a contact to a list

private

void ShowResults()

        {

           

string Url = String.Format("https://api.constantcontact.com/ws/customers/{0}/{1}", API_USERNAME, API_RESOURCE_LISTS);

 

           

CredentialCache CredentialCache = newCredentialCache();

            CredentialCache.Add(

newUri(Url), "Basic", newNetworkCredential(API_KEY + "%" + API_USERNAME, API_PASSWORD));

 

           

XmlUrlResolver XmlUrlResolver = newXmlUrlResolver { Credentials = CredentialCache };

           

XmlReaderSettings XmlReaderSettings = newXmlReaderSettings { XmlResolver = XmlUrlResolver };

 

           

XDocument Entries = XDocument.Load(XmlReader.Create(Url, XmlReaderSettings));

 

 

           

foreach (var Entry in Entries.Descendants(Atom + "entry"))

            {

               

if (OK(Convert.ToString(Entry)))

                {

                    lstbox_Display.Items.Add(

Convert.ToString(Entry.Element(Atom + "title").Value));

                }

            }

        }

           

publicbool OK(string newentry)

            {

               

string entry = newentry;

               

if(entry.Contains("Active"))

                   

returnfalse;

               

               

elseif(entry.Contains("Do Not Mail"))

                   

returnfalse;

               

elseif(entry.Contains("Removed"))

                   

returnfalse;

               

else


                   

returntrue;

            }

           

privatevoid lstbox_Display_MouseClick(object sender, MouseEventArgs e)

            {

               

if (lstbox_Display != null)

                {

                    txtbox_ListToAddContact.Text =

Convert.ToString(lstbox_Display.SelectedItem);

                }

            }

           

privatevoid btn_ContactSubmit_Click(object sender, EventArgs e)

            {

               

if(txtbox_ContactEmail != null && txtbox_ListToAddContact != null)

                {

                }

            }

        }

                               

   

publicstaticclassExtentionMethods


    {

       

publicstaticstring Post(thisXDocument XDocument, string Url, CredentialCache CredentialCache)

        {

           

WebClient WebClient = newWebClient();

            WebClient.Credentials = CredentialCache;

           

byte[] ByteArray = Encoding.UTF8.GetBytes

                (XDocument.ToString());

            WebClient.Encoding =

Encoding.UTF8;

            WebClient.Headers.Add(

HttpRequestHeader.ContentType, "application/atom+xml");

           

byte[] ResponseBuffer = WebClient.UploadData(Url, ByteArray);

           

returnEncoding.UTF8.GetString(ResponseBuffer);

        }

    }

}

 

 

 

So what this can do is add a list to CC and with the listbox i can display all the list in CC.  Also you can click an item within the listbox and it will populate the LIST TO textbox.  Now, what I want to be able to do is put an email in the ContactEmail textbox and submit both the contact list and contact email to ultimatly add the contact to the list.

 

Note* if this is possible would entering an email not associated with a contact automatically create the contact?

 

Any help furthering this code would be greatly appreciated.

Highlighted
Employee

Re: Adding a contact to a list

Hey Rachel,

 

You can definitely do what you want to do.  You would just take the list and add the appropriate nodes when you build your campaign XML.  You can find more information about what is necessary when creating your email Campaign XML here.

 

Where it asks for the contact lists you would just have it take the current List and store it into the XML before doing your POST. 

Ryan Davis
Quality Engineer
Highlighted
Advisor

Re: Adding a contact to a list

So your saying to add a contact to a list is creating a campaign.  Ok

Highlighted
Employee

Re: Adding a contact to a list

Hey Rachel,

 

When you add a contact to a list is the same process as adding a list to a campaign.  They are not the same type of collection and have many differences.

 

Ryan Davis
Quality Engineer
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