Allowing the user to subscribe again with C# API

SOLVED
Highlighted
Occasional Participant

Allowing the user to subscribe again with C# API

I've implemented the API with C# to add contacts to our mailing list. The problem is, after the user unsubscribes, he can't subscribe again. It throws an exception that the user already exist. How can I allow the user to be able to subscribe again?

 

Here is the code that I'm using:

 

ConstantContact cc = new ConstantContact(apiKey, accessToken);
            var contact = new CTCT.Components.Contacts.Contact();
            contact.FirstName = firstName;
            contact.LastName  = lastName;
            contact.EmailAddresses.Add(new CTCT.Components.Contacts.EmailAddress(email));
            
            var list = new CTCT.Components.Contacts.ContactList();
            list.Id = "144";
            contact.Lists.Add(list);

            try
            {
                cc.AddContact(contact, true);
            }
            catch (CTCT.Exceptions.OAuth2Exception oathEx)
            {
                return Json(oathEx.Message, JsonRequestBehavior.AllowGet);
            }
            catch (CTCT.Exceptions.CtctException)
            {
                return Json("Email address " + email + " already exists.", JsonRequestBehavior.AllowGet);
            }
            return Json("Success", JsonRequestBehavior.AllowGet);

 

Thanks,

Andrej

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Allowing the user to subscribe again with C# API

The API allows you to search for a Contact by email address. The SDKcall that allows you to do this is:

 

 ResultSet<Contact> GetContacts(string accessToken, string apiKey, string email, int? limit, DateTime? modifiedSince, ContactStatus? status);

 

Just pass in the parameter of string email with the correct value of the email you are looking to search for and it will return a single result if it is found, an empty result if not.   The found result will include the ID of the Contact you are looking to update.

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

4 REPLIES 4
Highlighted
Moderator

Re: Allowing the user to subscribe again with C# API

If the Contact already exists and is opted out, you would need to use the following method from the wrapper to Update the Contact:

 

public Contact UpdateContact(string accessToken, string apiKey, Contact contact, bool actionByVisitor)

 

The control for updating and Opted out Contacts it the actionBy value.  If set to True, this specifies that the Contact themselves conducted the action and will let you add the Contact to lists.  If it is set to Fales, this specifics that the Customer is making the request and it will not let you add a Contact to new lists.  Our Terms and Conditions of use require that you can only use this parameter as true if the Contact is making a direct request to your integration.  Otherwise, this is a violation of our T&C and could cause account termination.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Allowing the user to subscribe again with C# API

I tried to use:

 

cc.UpdateContact(contact, true);

 

which can be implemented in my code from my first post, but I get exception "You must provide a contact ID in order to update a contact.". I couldn't find a method where I can get the contact ID. There is a GetContact method, but it also requires contact ID as a parameter.

 

Thanks,

Andrej

Highlighted
Moderator

Re: Allowing the user to subscribe again with C# API

The API allows you to search for a Contact by email address. The SDKcall that allows you to do this is:

 

 ResultSet<Contact> GetContacts(string accessToken, string apiKey, string email, int? limit, DateTime? modifiedSince, ContactStatus? status);

 

Just pass in the parameter of string email with the correct value of the email you are looking to search for and it will return a single result if it is found, an empty result if not.   The found result will include the ID of the Contact you are looking to update.

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

Highlighted
Occasional Participant

Re: Allowing the user to subscribe again with C# API

That worked great, thanks!

Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured