Need to get a list of emails for Optouts and Bounces

cking
Regular Participant

Need to get a list of emails for Optouts and Bounces

Looking for any examples of how to pull a list of email addresses for optouts and bounces for a specific account. WORKING IN asp.NET/vb.NET.


Thank you,

Thank you,


Charles

16 REPLIES 16
atran88
Regular Participant

Hello,


You can retrieve the list of opt outs and bounces for a particular email event by performing an HTTP GET on the following URIs:


 


Optouts:


http://api.constantcontact.com/ws/customers/{UserName}/campaigns/{CampaignID}/events/optouts


 


Bounces:


http://api.constantcontact.com/ws/customers/{UserName}/campaigns/{CampaignID}/events/bounces


 


Additional information can be found HERE.

Andrew T

Support Engineer, Constant Contact

cking
Regular Participant

we have tried the suggested uri and validated that it is complete and correct. it contains the correct user name and campaign id. But our xml file contains only the following ines:


  <?xml version="1.0" standalone="yes" ?>


  <NewDataSet />


Here is our code


    Protected Sub ddCampaigns_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ConnectStr As String = _

        ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString


        'Try

        Dim ctu As String = GetCTU()

        Dim ctp As String = GetCTP()

        'check if user has ct's

        If String.IsNullOrEmpty(ctu) Then

            Exit Sub

        End If

        If String.IsNullOrEmpty(ctp) Then

            Exit Sub

        End If


        'this returns an xml info for selected Campaign (email)


        Dim strCampaignID As String

        Dim s As String = ddCampaigns.SelectedValue.ToString()

        Dim index As Integer = s.LastIndexOf("/")

        If index >= 0 And (index + 1) < s.Length Then

            strCampaignID = s.Substring(index + 1)

        End If


        Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & ctu & "/campaigns/" & strCampaignID & "/events/bounces" 'optouts" 'clicks"

        Dim sAPIKey As String = "///////"


        'setup httpWebReqeust to send the request

        Dim address As New Uri(sUri)

        Dim theRequest As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

        theRequest.Credentials = New NetworkCredential((sAPIKey & "%" & ctu), ctp)


        theRequest.Method = "GET"

        theRequest.ContentType = "application/vnd.ctct+xml"


        Using theResponse As HttpWebResponse = TryCast(theRequest.GetResponse(), HttpWebResponse)

            'load dataset with xml response

            Dim xtr As New XmlTextReader(theResponse.GetResponseStream())

            xtr.ReadString()

            Dim dscte As New DataSet()

            'dscte.ReadXml(xtr) ', XmlReadMode.IgnoreSchema)

            dscte.ReadXml(xtr, XmlReadMode.IgnoreSchema)




            dscte.WriteXml("C:\Documents\Data.xml")




        End Using

        'Catch ex As Exception

        '    HttpContext.Current.Response.Write(ex.Message)

        'End Try

    End Sub

Thank you,


Charles

cking
Regular Participant

We are now being told that the xml in your response is not correctly formatted. Can you provide any assistance?


The error is "Nested table 'link' which inherits its namespace cannot have multiple parent tables in different namespaces."



 

Thank you,


Charles

David_J
Employee

I apologize for the long response time on this post.


 


I am not an expert in VB, but it appears like your XmlTextReader 'xtr' is not getting populated with the data from 'theResponse' as the response stream as not been read. The response is being received correctly as your HttpWebResponse does have the appropriate content length and status code. If you adjust your code to something like:


 


Dim responseStream As Stream = theResponse.GetResponseStream()

Dim streamReader As StreamReader = New StreamReader(responseStream)

Dim responseText As String = streamReader.ReadToEnd


 


the responseText string will have the full XML content from your HttpWebRequest


 


I hope this helps point you in the right direction. Please let me know if you have any other questions or concerns.

David J

cking
Regular Participant

David,


Much appreciate your help with this. I tried your suggestion, however I now get an 'Illegal characters in path' error. Is the string returned to responseText in xml format?  The error is being returned on this line:

        Using theResponse As HttpWebResponse = TryCast(theRequest.GetResponse(), HttpWebResponse)


            Dim responseStream As Stream = theResponse.GetResponseStream()

            Dim streamReader As StreamReader = New StreamReader(responseStream)

            Dim responseText As String = streamReader.ReadToEnd


            Dim xtr As New XmlTextReader(responseText)

            xtr.ReadString()


            'load dataset with xml response

            Dim dscte As New DataSet()


            dscte.ReadXml(xtr) 'Illegal characters in path.


            'Create the xml file to check what we are receiving.

            dscte.WriteXml("C:\Documents\Data.xml")

        End Using


Thank you,


Charles


 



XmlTextReader(responseText)

Thank you,


Charles

cking
Regular Participant

Wanted you to also know that we were able to copy the content of the responseText to notepad and save it as an xml file. When we opened it in the browser, this is what we get: Not sure this any help.









The XML page cannot be displayed


Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.




Invalid at the top level of the document. Error processing resource 'file:///C:/Documents/test3.xml'. L...


"<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom">   <id>http://api.consta...



























Thank you,


Charles

Thank you,


Charles

David_J
Employee

I think I've got this working as you intended it to. I apologize again for the long response on this.


 


Dim responseStream As Stream = theResponse.GetResponseStream()

Dim xtr As New XmlTextReader(responseStream)

Dim dscte As New DataSet()

dscte.ReadXml(xtr)

dscte.WriteXml("C:\Documents\Data.xml")


 


Please let me know if this is working for you now?

David J

cking
Regular Participant

David,


Sorry, but we are now back to getting the original error: Nested table 'link' which inherits its namespace cannot have multiple parent tables in different namespaces.


The error is on the dscte.ReadXml(xtr) line.


I also wanted to add that using our origianl code we are able to successfuly retreive a list of our last 90 day campaigns and the events for the campaign. So my thinking is that there must be some difference in the structure of the bounce and optout xml response being returned. Perhaps some unusual formatting of the xml.


Thank you,


Charles

Thank you,


Charles

David_J
Employee

Hey Charles,


 


It appears that reading the XML into a dataset works fine for almost all GET's of our API, with the exception of campaign events (opens, clicks, optouts) that contain an actual occurance of an event. This is why it was working for me requesting certain calls where it was not for you.


 


<OpenEvent xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/djellesma2/events/opens/1103735175405,108,1285938830000">

        <Contact id="http://api.constantcontact.com/ws/customers/djellesma2/contacts/108">

          <EmailAddress>xxxxxx@xxxxx.com</EmailAddress>

          <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/djellesma2/contacts/108" rel="self"></link>

        </Contact>

        <Campaign id="http://api.constantcontact.com/ws/customers/djellesma2/campaigns/1103735175405">

          <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/djellesma2/campaigns/1103735175405" rel="self"></link>

          <Name>Oct 01 2010LV</Name>

        </Campaign>

        <EventTime>2010-10-01T13:13:50.000Z</EventTime>

      </OpenEvent>


 


I believe that this an issue with the DataSet class not being able to handle how deeply nested the link node is, but I am going to look further into this as I typically do use this type of approach to read the XML returned from a web request.

David J

cking
Regular Participant

David,


Thank you and I look forward to your reply and a resolution.


 


Charles

Thank you,


Charles

cking
Regular Participant

Good Morning David,


Any progress on this topic? We are eager to integrate this feature assoon as possible.


Thank you,


Charles

Thank you,


Charles

David_J
Employee

Hi Charles,


Unfortunately I'm not seeing a way to get the XML data read into a DataSet. The DataSet is attempting to load the XML into a relational structure of rows and columns, but it is unable to turn the XML returned from your API request into this row/column structure. There are other ways to get the XML data printed to a text file, such as reading the responseStream into a string like we had discussed a few posts up. The more important question is what is your goal for doing this? What are you trying to accomplish with the XML returned from us?

David J

cking
Regular Participant

Our goal is to present the user with separate lists of the email addresses for Bounces, Optouts, Clicks, and Opens for a selected campaign. This will allow us to then take the appropriate action in our system.


Thank you,


David

Thank you,


Charles

David_J
Employee

It you don't have a need to write the XML to a text file other than looking at it, the following code will store the XML response as the string 'responseText'. This could be written to a text file if you wish, or used elsewhere in your application?



Dim responseStream As Stream = theResponse.GetResponseStream()

Dim streamReader As StreamReader = New StreamReader(responseStream)

Dim responseText As String = streamReader.ReadToEnd


 


Is there a reason why you do not want to use a string to store the XML response?







 

David J

cking
Regular Participant

David,


At this point no, but I'm not sure how I would parse out just the email addresses from the string response.


Thank you,


Charles

Thank you,


Charles

cking
Regular Participant

David,


Just wanted you to know that we have been able to retieve the email addresses from the responseText string. I will contact you again should we require addtional assistance.


Thank you,


Charles

Thank you,


Charles

Developer Portal

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

Visit Page