C# remove contact from list

Highlighted
Occasional Participant

C# remove contact from list

Im a designer that has been thrown into a developer's position at work so I'm VERY new to this API integration. We are using ASP.NET and C#.


So far, what I have accomplished is editing  the web.config file to add the <appSettings> but I'm completely lost on the C# part of things. I've looked at the C# code samples and found them completely to complex for me to understand. What I'm looking to do is provide 3 checkboxes that correspond with the 3 contact list we have. If unchecked, it will remove the contact (we have there email stored that I have the ability to query using the API) from the contact list, hence them not getting the email anymore.


Now if you could provide me code that would be great! But im looking more for what to research to accomplish this. Do i have to research HTTPResponse or HTTPRequest etc. Do I have to install any assembly references or new .cs file into the App_code folder of my website?


Any help would be GREATLY appreciated!


 


Update:


I found some C# code that will pull in the contactID I need in order to remove the contact from the contact list:


 


protected void Page_Load(object sender, EventArgs e)

    {

        string myString = getDetails();

        Console.WriteLine(myString);

        Console.ReadLine();

        Label1.Text = myString;

    }



    public static string getDetails()

        {

 

        // Create LoginCreditials

        CredentialCache LoginCredentials = new CredentialCache();

 

         //Add a new credential for this account

        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/{username}"),// Set up URI for API site

         "Basic", // Basic login type

          new NetworkCredential("{API-Key}%{Username}", "{password}"));//UserName and Password

 

        // Create WebRequest, this is a factory class with a static constructor

        // You can not use new, all requests must be created with WebRequest.Create

        WebRequest Request = WebRequest.Create("https://api.constantcontact.com/ws/customers/{username}/contacts?email={emailaddress}");


// URI for the GET

 

        // Set Request credentials

        Request.Credentials = LoginCredentials;

        string XMLResponse;

 

        // Send GET request

        // Recast the response to HttpWebResponse for correct processing

        // Place in a try block to ensure that any errors are caught

        try

        {

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

        

            // Process the Response as needed

            // This is a generic StreamReader to read the entire response in

            // GetResponseStream can be recast to any of the Stream types

            // Note: Remember to add using System.IO

            StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string

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

        

            // Close Reader

            Reader.Close();

        

            // Close the response to free up the system resources

            Response.Close();

        

            return XMLResponse;

        }

        

        catch (WebException e)

        {

            // Get the web exception type, error number returned here

            return ("WebException:" + e.Status + "\n" + e.Message);

        }

        

        catch (Exception e)

        {

            // Get the exception type

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

        }

    }


 


The problem is I dont know how to extract the contactID from the above xmlstring. Any ideas?

8 REPLIES 8
Highlighted
Occasional Advisor

Hello,The XML returned from

Hello,


The XML returned from your HTTP GET is the following:


 


<?xml version="1.0" encoding="UTF-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">

  <id>http://api.constantcontact.com/ws/customers/{username}/contacts</id>

  <title type="text">Contacts for Customer: {username}</title>

  <link href="contacts"></link>

  <link href="contacts" rel="self"></link>

  <author>

    <name>{username}</name>

  </author>

  <updated>2010-12-22T18:07:07.967Z</updated>

  <link href="/ws/customers/{username}/contacts" rel="first"></link>

  <link href="/ws/customers/{username}/contacts?email={contactEmail}" rel="current"></link>

  <entry>

    <link href="/ws/customers/{username}/contacts/{id}" rel="edit"></link>

    <id>http://api.constantcontact.com/ws/customers/{username}/contacts/{id}</id>

    <title type="text">Contact: {contactEmail}</title>

    <updated>2010-12-22T18:07:08.036Z</updated>

    <author>

      <name>Constant Contact</name>

    </author>

    <content type="application/vnd.ctct+xml">

      <Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/{username}/contacts/{id}">

        <Status>Active</Status>

        <EmailAddress>{contactEmail}</EmailAddress>

        <EmailType>HTML</EmailType>

        <Name></Name>

        <OptInTime>2010-10-27T17:15:25.059Z</OptInTime>

        <OptInSource>ACTION_BY_CUSTOMER</OptInSource>

        <Confirmed>false</Confirmed>

        <InsertTime>2010-06-14T20:23:21.162Z</InsertTime>

      </Contact>

    </content>

  </entry>

</feed>


 


The contact ID can be found in the entry/id node. You can parse this data using various methods, such as xpath or XML-DOM.


In order to remove this contact from a specific list, you would have to perform an HTTP GET on the contact resource URI(which can be found through the link node, href attribute), and remove the contactlist element referring to the list you wish to remove your contact from the returned XML. Then, you would perform an HTTP PUT of this updated XML to the contact resource URI.


 


To completely remove your contact from all lists and place them in your Do Not Mail list, simply perform an HTTP DELETE to the contact resource URI.

Andrew T

Support Engineer, Constant Contact

Highlighted
Occasional Participant

Need help Parsing XML

Here is my c# code


 protected void Page_Load(object sender, EventArgs e)

    {

        string myString = getDetails();

        Console.WriteLine(myString);

        Console.ReadLine();

        Label1.Text = myString;

    }



    public static string getDetails()

        {



      



        // Create LoginCreditials

        CredentialCache LoginCredentials = new CredentialCache();

 

         //Add a new credential for this account

        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/"),// Set up URI for API site

         "Basic", // Basic login type

          new NetworkCredential("%", ""));//UserName and Password

 

        // Create WebRequest, this is a factory class with a static constructor

        // You can not use new, all requests must be created with WebRequest.Create

        WebRequest Request = WebRequest.Create("https://api.constantcontact.com/ws/customers//contacts?email="); // URI for the GET

 

        // Set Request credentials

        Request.Credentials = LoginCredentials;

        string XMLResponse;

 

        // Send GET request

        // Recast the response to HttpWebResponse for correct processing

        // Place in a try block to ensure that any errors are caught

        try

        {

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

        

            // Process the Response as needed

            // This is a generic StreamReader to read the entire response in

            // GetResponseStream can be recast to any of the Stream types

            // Note: Remember to add using System.IO

            StreamReader Reader = new StreamReader(Response.GetResponseStream()); // Read the entire XML response to a string string

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



        

            // Close Reader

            Reader.Close();

        

            // Close the response to free up the system resources

            Response.Close();



            return XMLResponse;

           

        }

        

        catch (WebException e)

        {

            // Get the web exception type, error number returned here

            return ("WebException:" + e.Status + "\n" + e.Message);

        }

        

        catch (Exception e)

        {

            // Get the exception type

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

        }



     



    }


 


This returns a sting that looks like this:


 


<?xml version="1.0" encoding="UTF-8"?>


<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://api.constantcontact.com/ws/customers//contacts</id>
<title type="text">Contacts for Customer: </title>
<link href="contacts"></link>
<link href="contacts" rel="self"></link>
<author>
<name></name>
</author>


<updated>2010-12-23T18:52:54.863Z</updated>
<link href="/ws/customers//contacts" rel="first"></link>
<link href="/ws/customers//contacts?email=" rel="current"></link>
<entry>
<link href="/ws/customers//contacts/" rel="edit"></link>
<id>http://api.constantcontact.com/ws/customers//contacts/</id>
<title type="text">Contact: </title>


<updated>2010-12-23T18:52:54.891Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers//contacts/">
<Status>Active</Status>


<EmailAddress></Name>
<OptInTime>2010-12-16T16:39:37.875Z</OptInTime>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<Confirmed>false</Confirmed>


<InsertTime>2010-11-01T16:33:57.213Z</InsertTime>
</Contact>
</content>
</entry>
</feed>
As it stand it is being populated into a Label so I can see if its pulling but now I cant parse the link out of
<link href="/ws/customers//contacts/" rel="edit"></link>
in the xml.

Can someone help me on this? Am I pulling the XML document wrong for what im trying to accomplish?
Highlighted
Occasional Advisor

Re: Deleting Contact

Hello,


 


What are you exactly trying to accomplish? Are you looking to unsubscribe a contact completely from all lists (ie Do Not Mail)? Or are you looking to remove the contact from a certain list(s)?

Andrew T

Support Engineer, Constant Contact

Highlighted
Occasional Participant

Delete from a contact List

We have 3 contact list that send out three different email blasts. All I need to do is delete the user from a particular list based off which checkbox is unchecked.


Thanks again for all the help!

Highlighted
Occasional Advisor

Re: Delete from a contact List

Did you ever resolve how to delete a contact from a single list?

Highlighted
Moderator

Re: Delete from a contact List

To remove a Contact from an individual list requires you to update a Contact.  One of the attributes of a Contact is their list membership.  To remove the Contact from one or more lists, just update the Contact with the ContactLists XML section no longer containing those lists.  For more information on this, see this documentation: http://community.constantcontact.com/t5/Documentation/Updating-Contact-Information/ba-p/25055

Dave Berard
Senior Product Manager, Constant Contact
Valued Developer

Re: Delete from a contact List

Dave,

 

Is this still the best method (API V2) to add/remove from a given list based on subscriber preference update?

~Clint
@cazoomi
Highlighted
Moderator

Re: Delete from a contact List

It is today.  We are looking into adding sub-resources for list management that will allow you to do more fine grained list management.  Will update when we have more information on this.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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