Adding a contact to a list in from scratch C#

SOLVED
Go to solution
RachelE81
Frequent Participant

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
RachelE81
Frequent Participant

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

View solution in original post

25 REPLIES 25
RachelE81
Frequent Participant

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

Hey Rachel,

 

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

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

Its ok thanks =)

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
RachelE81
Frequent Participant

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");

                    }

RachelE81
Frequent Participant

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?

RachelE81
Frequent Participant

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

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
RachelE81
Frequent Participant

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.

Hey Rachel,

 

Are you changing the list id to change it to the new list.  If you want to move it to the list you would just change:

XMLData += "<ContactLists>";
XMLData += "<ContactList id=\"http://api.constantcontact.com/ws/customers/{username}/lists/{listID}\"/>";
XMLData += "<ContactList id=\"http://api.constantcontact.com/ws/customers/{username}/lists/{listID}\"/>";
XMLData += "<ContactList id=\"http://api.constantcontact.com/ws/customers/{username}/lists/{listID}\"/>";
XMLData += "</ContactLists>";

 Where the listID can be supplied as many times as it needs to.  If you supply one list it will just move it to the the list.  If you supply many it will add it to all of the lists. 


If this is not working like this then it sounds like there is a problem with the put.  Please reply with the original data and tell me what you changed and supply the data that is being sent after you changed it.

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

ok maybe the fact that I am just using the email address as the user name could be the conflict when using the put? 

That could cause an error, but im not sure if it will result in the PUT not putting per say.  I would correct the synax and let me know the results.

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

Well I added the the same thing you did and no luck.... The contact is still located in the original list and not updated to the list i wanted.  I tried this both ways by adding

 

(List its in now)

                        XMLData +=

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

 

(List I want the contact also in)

                        XMLData +=

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

RachelE81
Frequent Participant

BTW all my code is in this forum... I have been supplying with hello@hello.com  for any place you see textbox.text

Hey Rachel,


Can you PM me the value of XMLDATA so I can see what is being submitted into the API.  I understand the code is there I just want to see if its building correctly.

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

Hey Ryan any news?

Hey Rachel,

 

I believe I sent an email to Jordan, when he emailed me at webservices.  I believe you are one and the same correct?  I attached some corrections but more work will need to be done to update the user. 

 

There were a few different errors that I noticed in your XML.  I am not so sure that you are using the best algorithm to do what you are doing.  But I am sending you back your form application with some minor changes so that it will do what it needs to do.  There is one more part that you must complete which is when the contact does exist and you have to do the put, you need to search for that contact and find out his ID.  Because when you do the put you need to do the put to that contacts id.  Such as https://api.constantcontact.com/ws/customers/{username}/contacts/{contactID}.  Once you do that and supply the PUT URI with the correct Contact ID you will be good to go.  As it stands you will get a 415 which says unsupported media type.  This is a bad error message but it does not mean exactly what it says.  You can not do a PUT to the /contacts only to the /contacts/{contactID} which makes sense as you cant change all your contacts in one pass.

Let me know if you have any questions.

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

Hmmm I havent gotten it yet?  Maybe a problem with out look... Anyway you could just send the text?

Hey Rachel,

 

Just sent it over again.

Ryan Davis
Quality Engineer
RachelE81
Frequent Participant

ok i understand what you mean.  I have hard coded the Contact ID in the url in the PUT "try" but I still get the error?

Developer Portal

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

Visit Page