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 in from scratch C#

SOLVED
Go to solution
Highlighted
Advisor

Adding a contact to a list in from scratch C#

         Please help :(  I tried to do this exactly like the API documentation.... Is there anything im missing or not adding.  I of course get the 400 error.

 

Thanks

 

 

 

privatevoid btn_ContactEmailSubmit_Click(object sender, EventArgs e)

            {

       if (txtbox_ContactEmail != null)

                {

        string NewListName = txtbox_ContactEmail.Text;

                   

        string Resource = API_RESOURCE_CONTACTS;

                   

        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 NewContact = newXDocument


(

                       

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

                       

 newXElement

 

   (

       Atom + "entry",

                               

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

                           

newXElement(Atom + "title=\"text\"", "" ),

                           

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

                           

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

                           

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

                           

newXElement(Atom + "summary",

         

           newXAttribute("type=\"text\"", "Contact")),

                           

newXElement(Atom + "content",

    

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

                           

newXElement(ConstantContact + "Contact",

 

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

                           

newXElement(ConstantContact + "EmailAddress", txtbox_ContactEmail.Text),

                           

newXElement(ConstantContact + "OptInSource", "ACTION_BY_CUSTOMER"),

                           

newXElement(ConstantContact + "ContactLists",

                           

newXElement(ConstantContact + "ContactList", newXAttribute("id", "https://api.constantcontact.com/ws/customers/{myusername}/lists/1"))

                            )

                            )

                            )

                            )

                            );

                       

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

                       

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

                        txtbox_ContactEmail.Clear();

                    }

                   

catch


                    {

                       

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

                    }

                }

            }

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

?? I really dont know what I can do on my end for this problem.

View solution in original post

25 REPLIES 25
Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

still dead in the water on this one.......

Highlighted
Employee

Re: Adding a contact to a list in from scratch C#

Hey Rachel,

 

I will look into this for you today. I apologize for the delay.

Ryan Davis
Quality Engineer
Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

Its ok thanks =)

Highlighted
Employee

Re: Adding a contact to a list in from scratch C#

Hey Rachel,

 

I think the problem with the application is that you are using HTTPS instead of HTTP when you are supplying your list ID.  Try replacing that and give it a try.  If that does not work please post the XML that you are trying to POST so I can troubleshoot it from there as I am not having much luck with Linq.

Ryan Davis
Quality Engineer
Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

Hey Ryan,  Thanks I dont know if that worked or I got it to work by adding @"" before my strings.  But i have it working.  Now i am trying to create a method within the catch that looks something like this

 

                   

try


{

                       

CredentialCache LoginCredentials = newCredentialCache();

                       

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

                        LoginCredentials.Add(

newUri(@https://api.constantcontact.com/ws/customers/username), "Basic", newNetworkCredential(API_KEY + @"%" + API_USERNAME, API_PASSWORD));

                       

HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(ContactURI);

                        Request.Method ="PUT";

                        Request.ContentType =@"application/atom+xml";

                        Request.Credentials = LoginCredentials;

                       

string XMLData = "";

                        XMLData +=

"<entry xmlns=\"http://www.w3.org/2005/Atom\">";

                        XMLData +=

@"<title></title>";

                        XMLData +=

@"<updated>2008-07-23T14:21:06.407Z</updated>";

                        XMLData +=

@"<author></author>";

                        XMLData +=

@"<id>data:,none</id>";

                        XMLData +=

@"<summary></summary>";

                        XMLData +=

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

                        XMLData +=

"<Contact xmlns=\"http://ws.constantcontact.com/ns/1.0/\">";

                        XMLData +=

"<EmailAddress>" + txtbox_ContactEmail.Text + "</EmailAddress>";

                        XMLData +=

"<FirstName>Hello</FirstName>";

                        XMLData +=

"<LastName>Test</LastName>";

                        XMLData +=

"<OptInSource>ACTION_BY_CUSTOMER</OptInSource>";

                        XMLData +=

"<ContactLists>";

                        XMLData +=

"<ContactList id=\"http://api.constantcontact.com/ws/customers//lists/28\"/>";

                        XMLData +=

"</ContactLists>";

                        XMLData +=

"</Contact>";

                        XMLData +=

"</content>";

                        XMLData +=

"</entry>";

                       

byte[] byteArray = Encoding.UTF8.GetBytes(XMLData);

                       

MessageBox.Show("You have successfully updated a Contact!");

                    }

                   

catch


                    {

                       

MessageBox.Show("Error");

                    }

Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

Now this will run and i will see the msg Contact Updated.... But i check CC and the contact is not added to the new list.....  I understand I have to do this put....Because the Post will not allow you to once you have a contact in CC. Any Advice?

Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

Not new list but already created list :D .  Trying not to confuse you

Highlighted
Employee

Re: Adding a contact to a list in from scratch C#

Hey Rachel,

 

I think the part that you are missing is that you need to include the original list with the new list.  A put will delete data that is not there.  It will update all of the original data with the new data that you are passing. So if you only supply the new list only the new list will be there.  If you supply multiple lists then multiple lists will be there.

 

Let me know if this helps.

Ryan Davis
Quality Engineer
Highlighted
Advisor

Re: Adding a contact to a list in from scratch C#

Hmmmm I am trying to follow you but I dont know if what your saying works for me.....  I understand that I should include the original list.  But if what your sayin is true then if I do a PUT then the contact should be deleted out of any other list that it is in and PUT in a new list of my choice...  I have worked it both ways and nothing changes.  It is still in the first list I POSTed it in and the list I tried to PUT it in has no contacts.

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