cancel
Showing results for 
Search instead for 
Did you mean: 

C# Error Checking if Contact Exists

Occasional Contributor

C# Error Checking if Contact Exists

I'm working with the C# Wrapper and trying to check if a contact exists in my Constant Contact contact list before adding it.  I'm tried many ways to check, but having trouble.  Below is what I have:

 

     private Contact GetContactByEmailAddress(string emailAddress, ConstantContact cc)
        {
            var result = cc.GetContacts(emailAddress,1,DateTime.Now.AddMonths(-1));
            MessageBox.Show("lookup2");
            if (result == null)
            {
                MessageBox.Show("lookup4");
                return null;
            }
            else
            {
                MessageBox.Show("lookup3");
                return result.Results[0];
            }
        }

 

The key question is what is returned when the contact does not exist in Constant Contact.  The code (result==null) does not work.  I've tried many ways to do this with no success.  I've tried (results.Results.Count == 0) too with no success.  Any assistance would be very helpful.

3 REPLIES 3
CTCT Employee

Re: C# Error Checking if Contact Exists

Hi,

 

I can see how that would be tricky.  Our http response contains a very minimal (but non-null) response in the case that no contacts were found for an email address:

 

{
    "meta": {
        "pagination": {}
    },
    "results": []
}

 

To check if a contact was returned, I'd recommend using a similar strategy that our contacts form example does: use a ResultSet, and see if that result set has any contacts in it, since its value will be null when no contacts are returned.

 

        private Contact GetContactByEmailAddress(string emailAddress)
        {
            ResultSet<Contact> contacts = _constantContact.GetContacts(emailAddress, 1, null);

            if (contacts != null)
            {
                if (contacts.Results != null && contacts.Results.Count > 0)
                {
                    return contacts.Results[0];
                }
            }

            return null;
        }

 

Best Regards,

Shannon W.

API Support Specialist

Occasional Contributor

Re: C# Error Checking if Contact Exists

Thanks

Moderator

Re: C# Error Checking if Contact Exists

The wrapper has been expanded upon to allow filtering by contact status since this sample was created. The current definition for the overload reflecting this change is:

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

And here is a code sample with changes to reflect this:

private Contact GetContactByEmailAddress(string emailAddress)
        {
            ResultSet<Contact> contacts = _constantContact.GetContacts(emailAddress, 1, null, null);

            if (contacts != null)
            {
                if (contacts.Results != null && contacts.Results.Count > 0)
                {
                    return contacts.Results[0];
                }
            }

            return null;
        }

 

Rich Marcucella
Sr. Technical Writer, Content Developer
Web Services Team
http://developer.constantcontact.com