Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

Constant Contact V3: Find a contact list by name

See more Constant Contact Examples

Start with the full list name from the Constant Contact account. Read its list_id and the number of active, mailable contacts. Use the ID for list membership operations or campaign targeting. This lookup requires contact_data.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkHttpRequestW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecretsW vault;
    vault.put_Location(L"local_manager");
    vault.put_AutoSplit(true);
    CkJsonObjectW entry;
    entry.UpdateString(L"appName",L"ConstantContactV3");
    entry.UpdateString(L"service",L"OAuth2");
    entry.UpdateString(L"username",L"my-account");
    CkJsonObjectW tokens;
    bool loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        wprintf(L"Run the desktop authorization example using this account and store.\n");
        return;
    }

    if (tokens.JsonTypeOf(L"access_token") != 1) {
        wprintf(L"The saved authorization has no access token.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf(L"access_token"));
    CkHttpResponseW resp;

    //  Use the full display name. AddParam encodes spaces and punctuation.
    const wchar_t *listName = L"Workshop announcements";
    CkHttpRequestW query;
    query.put_HttpVerb(L"GET");
    query.AddParam(L"name",listName);
    query.AddParam(L"status",L"active");
    query.AddParam(L"include_membership_count",L"active");
    success = http.HttpReq(L"https://api.cc.email/v3/contact_lists",query,resp);
    if ((success != true) && (resp.get_StatusCode() <= 0)) {
        wprintf(L"Look up list: HTTP request failed.\n");
        return;
    }

    if ((success != true) || (resp.get_StatusCode() != 200)) {
        wprintf(L"Look up list: HTTP %d %s\n",resp.get_StatusCode(),resp.bodyStr());
        return;
    }

    CkJsonObjectW matches;
    success = matches.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a JSON object in the response.\n");
        return;
    }

    if (matches.SizeOfArray(L"lists") == 0) {
        wprintf(L"No active email list named %s\n",listName);
        return;
    }

    if ((matches.SizeOfArray(L"lists") != 1) || (matches.StringOfEquals(L"lists[0].name",listName,true) != true)) {
        wprintf(L"Expected one exact list name.\n");
        return;
    }

    const wchar_t *listId = matches.stringOf(L"lists[0].list_id");
    if (listId == L"") {
        wprintf(L"Missing list ID.\n");
        return;
    }

    wprintf(L"%s | List ID: %s | Mailable contacts: %d\n",listName,listId,matches.IntOf(L"lists[0].membership_count"));
    }