Sample code for 30+ languages & platforms
Unicode 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 Unicode C++ Downloads

Unicode C++
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.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;

    const wchar_t *campaignName = L"Workshop newsletter";
    const wchar_t *campaignId = L"";
    CkStringBuilderW url;
    url.Append(L"https://api.cc.email/v3/emails?limit=50");
    CkJsonObjectW page;
    while (((url.get_Length() > 0) && (campaignId == L""))) {
        success = http.HttpNoBody(L"GET",url.getAsString(),resp);
        if ((success != true) && (resp.get_StatusCode() <= 0)) {
            wprintf(L"Read campaigns: HTTP request failed.\n");
            return;
        }

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

        success = page.Load(resp.bodyStr());
        int count = page.SizeOfArray(L"campaigns");
        if ((success != true) || (count < 0)) {
            wprintf(L"Expected a page of campaigns.\n");
            return;
        }

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

            i = i + 1;
        }

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

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

    }

    if (campaignId == L"") {
        wprintf(L"No campaign named %s\n",campaignName);
        return;
    }

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

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

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

    //  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
    CkJsonObjectW primary;
    success = campaign.FindRecord2(L"campaign_activities",L"role",L"primary_email",true,primary);
    if ((success != true) || (primary.JsonTypeOf(L"campaign_activity_id") != 1)) {
        wprintf(L"No primary email activity ID in the response.\n");
        return;
    }

    const wchar_t *selectedCampaign = campaign.stringOf(L"campaign_id");
    const wchar_t *selectedActivity = primary.stringOf(L"campaign_activity_id");
    if ((selectedCampaign == L"") || (selectedActivity == L"")) {
        wprintf(L"Missing campaign or activity ID.\n");
        return;
    }

    wprintf(L"Campaign ID: %s\n",selectedCampaign);
    wprintf(L"Primary activity ID: %s\n",selectedActivity);
    CkJsonObjectW selection;
    selection.UpdateString(L"name",campaign.stringOf(L"name"));
    selection.UpdateString(L"campaign_id",selectedCampaign);
    selection.UpdateString(L"campaign_activity_id",selectedActivity);
    success = selection.WriteFile(L"newsletter-selection.json");
    if (success != true) {
        wprintf(L"Could not save newsletter-selection.json. Keep the IDs printed above.\n");
        return;
    }

    wprintf(L"Saved newsletter-selection.json.\n");
    }