Sample code for 30+ languages & platforms
.NET Core C# Requires Chilkat v11.0.0+

Constant Contact V3: Find a campaign and its primary email activity

See more Constant Contact Examples

Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.

The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.

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();

string campaignName = "Workshop newsletter";
string campaignId = "";
Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://api.cc.email/v3/emails?limit=50");
Chilkat.JsonObject page = new Chilkat.JsonObject();
while (((url.Length > 0) && (campaignId == ""))) {
    success = http.HttpNoBody("GET",url.GetAsString(),resp);
    if ((success != true) && (resp.StatusCode <= 0)) {
        Debug.WriteLine("Read campaigns: HTTP request failed.");
        return;
    }

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

    success = page.Load(resp.BodyStr);
    int count = page.SizeOfArray("campaigns");
    if ((success != true) || (count < 0)) {
        Debug.WriteLine("Expected a page of campaigns.");
        return;
    }

    int i = 0;
    while (((i < count) && (campaignId == ""))) {
        page.I = i;
        if (page.StringOfEquals("campaigns[i].name",campaignName,true) == true) {
            campaignId = page.StringOf("campaigns[i].campaign_id");
        }

        i = i + 1;
    }

    url.Clear();
    if ((campaignId == "") && (page.JsonTypeOf("_links.next.href") == 1)) {
        url.Append(page.StringOf("_links.next.href"));
        if (url.StartsWith("/v3/emails?",true) != true) {
            Debug.WriteLine("Unexpected campaign pagination link.");
            return;
        }

        url.Prepend("https://api.cc.email");
    }

}

if (campaignId == "") {
    Debug.WriteLine("No campaign named " + campaignName);
    return;
}

http.SetUrlVar("campaign",campaignId);
success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",resp);
if ((success != true) && (resp.StatusCode <= 0)) {
    Debug.WriteLine("Read campaign: HTTP request failed.");
    return;
}

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

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

//  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
Chilkat.JsonObject primary = new Chilkat.JsonObject();
success = campaign.FindRecord2("campaign_activities","role","primary_email",true,primary);
if ((success != true) || (primary.JsonTypeOf("campaign_activity_id") != 1)) {
    Debug.WriteLine("No primary email activity ID in the response.");
    return;
}

string selectedCampaign = campaign.StringOf("campaign_id");
string selectedActivity = primary.StringOf("campaign_activity_id");
if ((selectedCampaign == "") || (selectedActivity == "")) {
    Debug.WriteLine("Missing campaign or activity ID.");
    return;
}

Debug.WriteLine("Campaign ID: " + selectedCampaign);
Debug.WriteLine("Primary activity ID: " + selectedActivity);
Chilkat.JsonObject selection = new Chilkat.JsonObject();
selection.UpdateString("name",campaign.StringOf("name"));
selection.UpdateString("campaign_id",selectedCampaign);
selection.UpdateString("campaign_activity_id",selectedActivity);
success = selection.WriteFile("newsletter-selection.json");
if (success != true) {
    Debug.WriteLine("Could not save newsletter-selection.json. Keep the IDs printed above.");
    return;
}

Debug.WriteLine("Saved newsletter-selection.json.");