Sample code for 30+ languages & platforms
.NET Core 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 .NET Core C# Downloads

.NET Core C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
Chilkat.Secrets vault = new Chilkat.Secrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
Chilkat.JsonObject entry = new Chilkat.JsonObject();
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
Chilkat.JsonObject tokens = new Chilkat.JsonObject();
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
    Debug.WriteLine("Run the desktop authorization example using this account and store.");
    return;
}

if (tokens.JsonTypeOf("access_token") != 1) {
    Debug.WriteLine("The saved authorization has no access token.");
    return;
}

Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = tokens.StringOf("access_token");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();

//  Use the full display name. AddParam encodes spaces and punctuation.
string listName = "Workshop announcements";
Chilkat.HttpRequest query = new Chilkat.HttpRequest();
query.HttpVerb = "GET";
query.AddParam("name",listName);
query.AddParam("status","active");
query.AddParam("include_membership_count","active");
success = http.HttpReq("https://api.cc.email/v3/contact_lists",query,resp);
if ((success != true) && (resp.StatusCode <= 0)) {
    Debug.WriteLine("Look up list: HTTP request failed.");
    return;
}

if ((success != true) || (resp.StatusCode != 200)) {
    Debug.WriteLine("Look up list: HTTP " + Convert.ToString(resp.StatusCode) + " " + resp.BodyStr);
    return;
}

Chilkat.JsonObject matches = new Chilkat.JsonObject();
success = matches.Load(resp.BodyStr);
if (success != true) {
    Debug.WriteLine("Expected a JSON object in the response.");
    return;
}

if (matches.SizeOfArray("lists") == 0) {
    Debug.WriteLine("No active email list named " + listName);
    return;
}

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

string listId = matches.StringOf("lists[0].list_id");
if (listId == "") {
    Debug.WriteLine("Missing list ID.");
    return;
}

Debug.WriteLine(listName + " | List ID: " + listId + " | Mailable contacts: " + Convert.ToString(matches.IntOf("lists[0].membership_count")));