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

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.Secrets,
  Chilkat.StringBuilder,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  campaignName: string;
  campaignId: string;
  url: TStringBuilder;
  page: TJsonObject;
  count: Integer;
  i: Integer;
  campaign: TJsonObject;
  primary: TJsonObject;
  selectedCampaign: string;
  selectedActivity: string;
  selection: TJsonObject;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  vault := TSecrets.Create;
  vault.Location := 'local_manager';
  vault.AutoSplit := True;
  entry := TJsonObject.Create;
  entry.UpdateString('appName','ConstantContactV3');
  entry.UpdateString('service','OAuth2');
  entry.UpdateString('username','my-account');
  tokens := TJsonObject.Create;
  loaded := vault.GetSecretJson(entry,tokens);
  if (loaded <> True) then
    begin
      WriteLn('Run the desktop authorization example using this account and store.');
      Exit;
    end;
  if (tokens.JsonTypeOf('access_token') <> 1) then
    begin
      WriteLn('The saved authorization has no access token.');
      Exit;
    end;
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  http.AuthToken := tokens.StringOf('access_token');
  resp := THttpResponse.Create;

  campaignName := 'Workshop newsletter';
  campaignId := '';
  url := TStringBuilder.Create;
  url.Append('https://api.cc.email/v3/emails?limit=50');
  page := TJsonObject.Create;
  while ((url.Length > 0) and (campaignId = '')) do
    begin
      success := http.HttpNoBody('GET',url.GetAsString(),resp);
      if ((success <> True) and (resp.StatusCode <= 0)) then
        begin
          WriteLn('Read campaigns: HTTP request failed.');
          Exit;
        end;
      if ((success <> True) or (resp.StatusCode <> 200)) then
        begin
          WriteLn('Read campaigns: HTTP ' + resp.StatusCode + ' ' + resp.BodyStr);
          Exit;
        end;
      success := page.Load(resp.BodyStr);
      count := page.SizeOfArray('campaigns');
      if ((success <> True) or (count < 0)) then
        begin
          WriteLn('Expected a page of campaigns.');
          Exit;
        end;
      i := 0;
      while ((i < count) and (campaignId = '')) do
        begin
          page.I := i;
          if (page.StringOfEquals('campaigns[i].name',campaignName,True) = True) then
            begin
              campaignId := page.StringOf('campaigns[i].campaign_id');
            end;
          i := i + 1;
        end;

      url.Clear();
      if ((campaignId = '') and (page.JsonTypeOf('_links.next.href') = 1)) then
        begin
          url.Append(page.StringOf('_links.next.href'));
          if (url.StartsWith('/v3/emails?',True) <> True) then
            begin
              WriteLn('Unexpected campaign pagination link.');
              Exit;
            end;
          url.Prepend('https://api.cc.email');
        end;
    end;

  if (campaignId = '') then
    begin
      WriteLn('No campaign named ' + campaignName);
      Exit;
    end;
  http.SetUrlVar('campaign',campaignId);
  success := http.HttpNoBody('GET','https://api.cc.email/v3/emails/{$campaign}',resp);
  if ((success <> True) and (resp.StatusCode <= 0)) then
    begin
      WriteLn('Read campaign: HTTP request failed.');
      Exit;
    end;
  if ((success <> True) or (resp.StatusCode <> 200)) then
    begin
      WriteLn('Read campaign: HTTP ' + resp.StatusCode + ' ' + resp.BodyStr);
      Exit;
    end;
  campaign := TJsonObject.Create;
  success := campaign.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Expected a JSON object in the response.');
      Exit;
    end;
  //  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
  primary := TJsonObject.Create;
  success := campaign.FindRecord2('campaign_activities','role','primary_email',True,primary);
  if ((success <> True) or (primary.JsonTypeOf('campaign_activity_id') <> 1)) then
    begin
      WriteLn('No primary email activity ID in the response.');
      Exit;
    end;
  selectedCampaign := campaign.StringOf('campaign_id');
  selectedActivity := primary.StringOf('campaign_activity_id');
  if ((selectedCampaign = '') or (selectedActivity = '')) then
    begin
      WriteLn('Missing campaign or activity ID.');
      Exit;
    end;
  WriteLn('Campaign ID: ' + selectedCampaign);
  WriteLn('Primary activity ID: ' + selectedActivity);
  selection := TJsonObject.Create;
  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) then
    begin
      WriteLn('Could not save newsletter-selection.json. Keep the IDs printed above.');
      Exit;
    end;
  WriteLn('Saved newsletter-selection.json.');


  vault.Free;
  entry.Free;
  tokens.Free;
  http.Free;
  resp.Free;
  url.Free;
  page.Free;
  campaign.Free;
  primary.Free;
  selection.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.