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

PowerShell
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"

$success = $false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","ConstantContactV3")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","my-account")
$tokens = New-Object Chilkat.JsonObject
$loaded = $vault.GetSecretJson($entry,$tokens)
if ($loaded -ne $true) {
    $("Run the desktop authorization example using this account and store.")
    exit
}

if ($tokens.JsonTypeOf("access_token") -ne 1) {
    $("The saved authorization has no access token.")
    exit
}

$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $tokens.StringOf("access_token")
$resp = New-Object Chilkat.HttpResponse

$campaignName = "Workshop newsletter"
$campaignId = ""
$url = New-Object Chilkat.StringBuilder
$url.Append("https://api.cc.email/v3/emails?limit=50")
$page = New-Object Chilkat.JsonObject
while ((($url.Length -gt 0) -and ($campaignId -eq ""))) {
    $success = $http.HttpNoBody("GET",$url.GetAsString(),$resp)
    if (($success -ne $true) -and ($resp.StatusCode -le 0)) {
        $("Read campaigns: HTTP request failed.")
        exit
    }

    if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
        $("Read campaigns: HTTP " + $resp.StatusCode + " " + $resp.BodyStr)
        exit
    }

    $success = $page.Load($resp.BodyStr)
    $count = $page.SizeOfArray("campaigns")
    if (($success -ne $true) -or ($count -lt 0)) {
        $("Expected a page of campaigns.")
        exit
    }

    $i = 0
    while ((($i -lt $count) -and ($campaignId -eq ""))) {
        $page.I = $i
        if ($page.StringOfEquals("campaigns[i].name",$campaignName,$true) -eq $true) {
            $campaignId = $page.StringOf("campaigns[i].campaign_id")
        }

        $i = $i + 1
    }

    $url.Clear()
    if (($campaignId -eq "") -and ($page.JsonTypeOf("_links.next.href") -eq 1)) {
        $url.Append($page.StringOf("_links.next.href"))
        if ($url.StartsWith("/v3/emails?",$true) -ne $true) {
            $("Unexpected campaign pagination link.")
            exit
        }

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

}

if ($campaignId -eq "") {
    $("No campaign named " + $campaignName)
    exit
}

$http.SetUrlVar("campaign",$campaignId)
$success = $http.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",$resp)
if (($success -ne $true) -and ($resp.StatusCode -le 0)) {
    $("Read campaign: HTTP request failed.")
    exit
}

if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
    $("Read campaign: HTTP " + $resp.StatusCode + " " + $resp.BodyStr)
    exit
}

$campaign = New-Object Chilkat.JsonObject
$success = $campaign.Load($resp.BodyStr)
if ($success -ne $true) {
    $("Expected a JSON object in the response.")
    exit
}

#  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
$primary = New-Object Chilkat.JsonObject
$success = $campaign.FindRecord2("campaign_activities","role","primary_email",$true,$primary)
if (($success -ne $true) -or ($primary.JsonTypeOf("campaign_activity_id") -ne 1)) {
    $("No primary email activity ID in the response.")
    exit
}

$selectedCampaign = $campaign.StringOf("campaign_id")
$selectedActivity = $primary.StringOf("campaign_activity_id")
if (($selectedCampaign -eq "") -or ($selectedActivity -eq "")) {
    $("Missing campaign or activity ID.")
    exit
}

$("Campaign ID: " + $selectedCampaign)
$("Primary activity ID: " + $selectedActivity)
$selection = New-Object 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 -ne $true) {
    $("Could not save newsletter-selection.json. Keep the IDs printed above.")
    exit
}

$("Saved newsletter-selection.json.")