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.

Getting members from a contact list via API

Highlighted
Advisor

Getting members from a contact list via API

Hi there,

 

I am getting a list of members from a contact via API. When I save the xml file, all I see is Email and Name. There are some other fields i want to bring as well. How would I do that? 

 

Here is my code to get the xml file from ctct.

string uName = "user";
            string pWord = "password";
            string APIKey = "key";
            CredentialCache LoginCredentials = new CredentialCache();
            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +
                uName + "/lists/12/members");
            request.Credentials = LoginCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream str = response.GetResponseStream();
            byte[] inBuf = new byte[100000];

            try
            {
                int bytesToRead = Convert.ToInt32(inBuf.Length);
                int bytesRead = 0;
                while (bytesToRead > 0)
                {
                    int n = str.Read(inBuf, bytesRead, bytesToRead);
                    if (n == 0)
                    {
                        break; // TODO: might not be correct. Was : Exit While
                    }
                    bytesRead += n;
                    bytesToRead -= n;
                }
                FileStream fstr = new FileStream(@"\\fileserver\driver\Ctct\ContactMembers.xml", FileMode.OpenOrCreate, FileAccess.Write);
                fstr.Write(inBuf, 0, bytesRead);
                str.Close();
                fstr.Close();
            }

            catch (Exception ex)
            {
                throw new Exception("Exception: " + ex.Message);
            }

 

 

Also, what happens if there are 300 members in the contact list? Will everything show up on the xml file? Not sure if it only takes 50 at a time?

 

Thanks in advance.

3 REPLIES 3
Highlighted
Trusted Contributor

Re: Getting members from a contact list via API

Hello,

 

API requests for list members returns only the email address, name, and the URL for each contact as the value of the id attribute in the ContactListMemeber element of each Entry node.  The response will include a page of the first 50 contacts, and several link elements that include href attributes whose value is a URL that you use for an API request to retrieve the next 50, previous 50, or the current list.  

 

To retrieve details for a given contact, you need to use the URL for that contact in an call to the Contacts Collection API, which is appropriate if your activity is for up to 25 contacts, but you should use the Bulk Activities API to request additional details if your activity is for a large number of contacts (over 25).  The bulk Activities API returned formatted XML that includes information on whether your request was successful, and if so also gives you the URL where you can download the exported file (CSV or TXT format).  You'd need to include functions in your application to get and parse the information from the exported file.

 

I hope this information is a helpful start.  Let us know if you have any additional questions.

 

Cheers,

Mark Coleman
Support Engineer
Highlighted
Occasional Participant

Re: Getting members from a contact list via API

Is the 50 contacts per page a new thing? I could swear it used to pull down all contacts.

Highlighted
Moderator

Re: Getting members from a contact list via API

In our REST API with a GET, 50 contacts per page is how we have always implemented this API.  Our Activities API allows you to queue up a bulk export activity, which is asyncronous CSV export, and then pull down a full list of all the contacts in a specific list in CSV format once that is complete.

Dave Berard
Senior Product Manager, Constant Contact
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