Delphi DLL Requires Chilkat v11.0.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
campaignName: PWideChar;
campaignId: PWideChar;
url: HCkStringBuilder;
page: HCkJsonObject;
count: Integer;
i: Integer;
campaign: HCkJsonObject;
primary: HCkJsonObject;
selectedCampaign: PWideChar;
selectedActivity: PWideChar;
selection: HCkJsonObject;
begin
success := False;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','ConstantContactV3');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','my-account');
tokens := CkJsonObject_Create();
loaded := CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded <> True) then
begin
Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
Exit;
end;
if (CkJsonObject_JsonTypeOf(tokens,'access_token') <> 1) then
begin
Memo1.Lines.Add('The saved authorization has no access token.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(tokens,'access_token'));
resp := CkHttpResponse_Create();
campaignName := 'Workshop newsletter';
campaignId := '';
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.cc.email/v3/emails?limit=50');
page := CkJsonObject_Create();
while ((CkStringBuilder_getLength(url) > 0) and (campaignId = '')) do
begin
success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
begin
Memo1.Lines.Add('Read campaigns: HTTP request failed.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Read campaigns: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
success := CkJsonObject_Load(page,CkHttpResponse__bodyStr(resp));
count := CkJsonObject_SizeOfArray(page,'campaigns');
if ((success <> True) or (count < 0)) then
begin
Memo1.Lines.Add('Expected a page of campaigns.');
Exit;
end;
i := 0;
while ((i < count) and (campaignId = '')) do
begin
CkJsonObject_putI(page,i);
if (CkJsonObject_StringOfEquals(page,'campaigns[i].name',campaignName,True) = True) then
begin
campaignId := CkJsonObject__stringOf(page,'campaigns[i].campaign_id');
end;
i := i + 1;
end;
CkStringBuilder_Clear(url);
if ((campaignId = '') and (CkJsonObject_JsonTypeOf(page,'_links.next.href') = 1)) then
begin
CkStringBuilder_Append(url,CkJsonObject__stringOf(page,'_links.next.href'));
if (CkStringBuilder_StartsWith(url,'/v3/emails?',True) <> True) then
begin
Memo1.Lines.Add('Unexpected campaign pagination link.');
Exit;
end;
CkStringBuilder_Prepend(url,'https://api.cc.email');
end;
end;
if (campaignId = '') then
begin
Memo1.Lines.Add('No campaign named ' + campaignName);
Exit;
end;
CkHttp_SetUrlVar(http,'campaign',campaignId);
success := CkHttp_HttpNoBody(http,'GET','https://api.cc.email/v3/emails/{$campaign}',resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
begin
Memo1.Lines.Add('Read campaign: HTTP request failed.');
Exit;
end;
if ((success <> True) or (CkHttpResponse_getStatusCode(resp) <> 200)) then
begin
Memo1.Lines.Add('Read campaign: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
Exit;
end;
campaign := CkJsonObject_Create();
success := CkJsonObject_Load(campaign,CkHttpResponse__bodyStr(resp));
if (success <> True) then
begin
Memo1.Lines.Add('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 := CkJsonObject_Create();
success := CkJsonObject_FindRecord2(campaign,'campaign_activities','role','primary_email',True,primary);
if ((success <> True) or (CkJsonObject_JsonTypeOf(primary,'campaign_activity_id') <> 1)) then
begin
Memo1.Lines.Add('No primary email activity ID in the response.');
Exit;
end;
selectedCampaign := CkJsonObject__stringOf(campaign,'campaign_id');
selectedActivity := CkJsonObject__stringOf(primary,'campaign_activity_id');
if ((selectedCampaign = '') or (selectedActivity = '')) then
begin
Memo1.Lines.Add('Missing campaign or activity ID.');
Exit;
end;
Memo1.Lines.Add('Campaign ID: ' + selectedCampaign);
Memo1.Lines.Add('Primary activity ID: ' + selectedActivity);
selection := CkJsonObject_Create();
CkJsonObject_UpdateString(selection,'name',CkJsonObject__stringOf(campaign,'name'));
CkJsonObject_UpdateString(selection,'campaign_id',selectedCampaign);
CkJsonObject_UpdateString(selection,'campaign_activity_id',selectedActivity);
success := CkJsonObject_WriteFile(selection,'newsletter-selection.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not save newsletter-selection.json. Keep the IDs printed above.');
Exit;
end;
Memo1.Lines.Add('Saved newsletter-selection.json.');
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);
CkJsonObject_Dispose(campaign);
CkJsonObject_Dispose(primary);
CkJsonObject_Dispose(selection);