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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  final vault = CkSecrets();
  vault.location = 'local_manager';
  vault.autoSplit = true;
  final entry = CkJsonObject();
  entry.updateString('appName', 'ConstantContactV3');
  entry.updateString('service', 'OAuth2');
  entry.updateString('username', 'my-account');
  final tokens = CkJsonObject();
  try {
    vault.getSecretJson(entry, tokens);
  } on ChilkatException {
    print('Run the desktop authorization example using this account and store.');
    return;
  }

  if (tokens.jsonTypeOf('access_token') != 1) {
    print('The saved authorization has no access token.');
    return;
  }

  final http = CkHttp();
  http.requireSslCertVerify = true;
  http.authToken = tokens.stringOf('access_token');
  final resp = CkHttpResponse();

  final campaignName = 'Workshop newsletter';
  var campaignId = '';
  final url = CkStringBuilder();
  url.append('https://api.cc.email/v3/emails?limit=50');
  final page = CkJsonObject();
  while ((url.length > 0) && (campaignId == '')) {
    success = true;
    try {
      http.httpNoBody('GET', url.getAsString(), resp);
    } on ChilkatException {
      success = false;
    }
    if ((!success) && (resp.statusCode <= 0)) {
      print('Read campaigns: HTTP request failed.');
      return;
    }

    if ((!success) || (resp.statusCode != 200)) {
      print('Read campaigns: HTTP ${resp.statusCode} ${resp.bodyStr}');
      return;
    }

    success = true;
    try {
      page.load(resp.bodyStr);
    } on ChilkatException {
      success = false;
    }
    final count = page.sizeOfArray('campaigns');
    if ((!success) || (count < 0)) {
      print('Expected a page of campaigns.');
      return;
    }

    var i = 0;
    while ((i < count) && (campaignId == '')) {
      page.i = i;
      if (page.stringOfEquals('campaigns[i].name', campaignName, true)) {
        campaignId = page.stringOf('campaigns[i].campaign_id');
      }

      i++;
    }

    url.clear();
    if ((campaignId == '') && (page.jsonTypeOf('_links.next.href') == 1)) {
      url.append(page.stringOf('_links.next.href'));
      if (!url.startsWith('/v3/emails?', true)) {
        print('Unexpected campaign pagination link.');
        return;
      }

      url.prepend('https://api.cc.email');
    }
  }

  if (campaignId == '') {
    print('No campaign named $campaignName');
    return;
  }

  http.setUrlVar('campaign', campaignId);
  success = true;
  try {
    http.httpNoBody('GET', 'https://api.cc.email/v3/emails/{\$campaign}', resp);
  } on ChilkatException {
    success = false;
  }
  if ((!success) && (resp.statusCode <= 0)) {
    print('Read campaign: HTTP request failed.');
    return;
  }

  if ((!success) || (resp.statusCode != 200)) {
    print('Read campaign: HTTP ${resp.statusCode} ${resp.bodyStr}');
    return;
  }

  final campaign = CkJsonObject();
  success = true;
  try {
    campaign.load(resp.bodyStr);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Expected a JSON object in the response.');
    return;
  }

  // The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
  final primary = CkJsonObject();
  success = true;
  try {
    campaign.findRecord2('campaign_activities', 'role', 'primary_email', true, primary);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || (primary.jsonTypeOf('campaign_activity_id') != 1)) {
    print('No primary email activity ID in the response.');
    return;
  }

  final selectedCampaign = campaign.stringOf('campaign_id');
  final selectedActivity = primary.stringOf('campaign_activity_id');
  if ((selectedCampaign == '') || (selectedActivity == '')) {
    print('Missing campaign or activity ID.');
    return;
  }

  print('Campaign ID: $selectedCampaign');
  print('Primary activity ID: $selectedActivity');
  final selection = CkJsonObject();
  selection.updateString('name', campaign.stringOf('name'));
  selection.updateString('campaign_id', selectedCampaign);
  selection.updateString('campaign_activity_id', selectedActivity);
  success = true;
  try {
    selection.writeFile('newsletter-selection.json');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not save newsletter-selection.json. Keep the IDs printed above.');
    return;
  }

  print('Saved newsletter-selection.json.');
}