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.

Trouble with adding a contact list

Highlighted
Occasional Participant

Trouble with adding a contact list

I am trying to add a list to a contact using VBA with MSAccess (& XMLHTTP):

 

1.) I first do a GET for a contact and use the Getobj.response.xml to add the list reference

 

2.) I create a node (nNode) pointing to the /entry/content/Contact/ContactLists

 

3.) I create a string for a new element:

<ContactList id="http://api.constantcontact.com/ws/customers/[removed for security]/lists/8 >
<OptInSource>ACTION_BY_CUSTOMER</OptInSource></ContactList>


Here is the code I used to add the child node (which seems to work)

xmlstr = "<ContactList id=""http://api.constantcontact.com/ws/customers/" & MyUserName & "/lists/" & Format(SRA_List(i).URI) & " >" & vbCrLf _
                    & "<OptInSource>ACTION_BY_CUSTOMER</OptInSource>" & cbCRLF _
                    & "</ContactList>"

                Set elem = xmlDoc.createElement("ContactList")
                elem.Text = xmlstr
                Set Pelem = nNode
                Pelem.appendChild elem

 The section of the body (which has now been modified) used in the PUT:

<CustomField15></CustomField15>
            <ContactLists>
                <ContactList id="http://api.constantcontact.com/ws/customers/[removed for security]/lists/3">
                    <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/randyneilson/lists/3" rel="self"></link>
                    <OptInSource>ACTION_BY_CONTACT</OptInSource>
                    <OptInTime>2013-03-27T02:35:15.649Z</OptInTime>
                </ContactList>
                <ContactList id="http://api.constantcontact.com/ws/customers/[removed for security]/lists/7">
                    <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/randyneilson/lists/7" rel="self"></link>
                    <OptInSource>ACTION_BY_CONTACT</OptInSource>
                    <OptInTime>2013-03-27T02:35:15.649Z</OptInTime>
                </ContactList>
                <ContactList xmlns="">&lt;ContactList id="http://api.constantcontact.com/ws/customers/[removed for security]/lists/8 &gt;
&lt;OptInSource&gt;ACTION_BY_CUSTOMER&lt;/OptInSource&gt;&lt;/ContactList&gt;</ContactList></ContactLists>
            <Confirmed>true</Confirmed>

 

4.) Although this doesn't look pretty, the put is successful with a status of 204 - No Content. However when I do a GET of that contact, the new list is not added.

 

So I must be doing something wrong with adding the XML node for the new contactlist.

 

Any suggestions? or ideas?

Thanks

1 REPLY 1
Highlighted
Honored Contributor

Re: Trouble with adding a contact list

Hello,

 

When you're adding lists to a contact, it's important to follow the given format for adding a list. There are a few things happening here when your code is run:

 

1. Your xmlStr that you are adding as text is being encoded with entities and is being simply added as text rather than XML code. Most likely you would need to create new XML elements for each piece, or there may be a field that you can set which won't change characters such as '<' and '>' to their HTML entity equivalents.


2. Your XML has two <ContactList> elements where only one is needed. See below for an example of the given format vs. what you have created:

Given Format:
<ContactList id="http://api.constantcontact.com/ws/customers/joesflowers/lists/1" />

Your Format:
<ContactList xmlns="">
<ContactList id="http://api.constantcontact.com/ws/customers/randyneilson/lists/8 >
<OptInSource>ACTION_BY_CUSTOMER</OptInSource></ContactList>
</ContactList>

 

If you can modify what you have to create the correct XML, you should see the new list created successfully.

 

Sincerely,

Elijah G.

Tier 2 Support

 

Elijah G.
API Support 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