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

Visual Basic 6.0
Dim success As Long
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New ChilkatSecrets
vault.Location = "local_manager"
vault.AutoSplit = 1
Dim entry As New ChilkatJsonObject
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
Dim tokens As New ChilkatJsonObject
Dim loaded As Long
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
    Debug.Print "Run the desktop authorization example using this account and store."
    Exit Sub
End If

If (tokens.JsonTypeOf("access_token") <> 1) Then
    Debug.Print "The saved authorization has no access token."
    Exit Sub
End If

Dim http As New ChilkatHttp
http.RequireSslCertVerify = 1
http.AuthToken = tokens.StringOf("access_token")
Dim resp As New ChilkatHttpResponse

Dim campaignName As String
campaignName = "Workshop newsletter"
Dim campaignId As String
campaignId = ""
Dim url As New ChilkatStringBuilder
success = url.Append("https://api.cc.email/v3/emails?limit=50")
Dim page As New ChilkatJsonObject
Do While ((url.Length > 0) And (campaignId = ""))
    success = http.HttpNoBody("GET",url.GetAsString(),resp)
    If ((success <> 1) And (resp.StatusCode <= 0)) Then
        Debug.Print "Read campaigns: HTTP request failed."
        Exit Sub
    End If

    If ((success <> 1) Or (resp.StatusCode <> 200)) Then
        Debug.Print "Read campaigns: HTTP " & resp.StatusCode & " " & resp.BodyStr
        Exit Sub
    End If

    success = page.Load(resp.BodyStr)
    Dim count As Long
    count = page.SizeOfArray("campaigns")
    If ((success <> 1) Or (count < 0)) Then
        Debug.Print "Expected a page of campaigns."
        Exit Sub
    End If

    Dim i As Long
    i = 0
    Do While ((i < count) And (campaignId = ""))
        page.I = i
        If (page.StringOfEquals("campaigns[i].name",campaignName,1) = 1) Then
            campaignId = page.StringOf("campaigns[i].campaign_id")
        End If

        i = i + 1
    Loop
    url.Clear 
    If ((campaignId = "") And (page.JsonTypeOf("_links.next.href") = 1)) Then
        success = url.Append(page.StringOf("_links.next.href"))
        If (url.StartsWith("/v3/emails?",1) <> 1) Then
            Debug.Print "Unexpected campaign pagination link."
            Exit Sub
        End If

        success = url.Prepend("https://api.cc.email")
    End If

Loop
If (campaignId = "") Then
    Debug.Print "No campaign named " & campaignName
    Exit Sub
End If

success = http.SetUrlVar("campaign",campaignId)
success = http.HttpNoBody("GET","https://api.cc.email/v3/emails/{$campaign}",resp)
If ((success <> 1) And (resp.StatusCode <= 0)) Then
    Debug.Print "Read campaign: HTTP request failed."
    Exit Sub
End If

If ((success <> 1) Or (resp.StatusCode <> 200)) Then
    Debug.Print "Read campaign: HTTP " & resp.StatusCode & " " & resp.BodyStr
    Exit Sub
End If

Dim campaign As New ChilkatJsonObject
success = campaign.Load(resp.BodyStr)
If (success <> 1) Then
    Debug.Print "Expected a JSON object in the response."
    Exit Sub
End If

'  The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
Dim primary As New ChilkatJsonObject
success = campaign.FindRecord2("campaign_activities","role","primary_email",1,primary)
If ((success <> 1) Or (primary.JsonTypeOf("campaign_activity_id") <> 1)) Then
    Debug.Print "No primary email activity ID in the response."
    Exit Sub
End If

Dim selectedCampaign As String
selectedCampaign = campaign.StringOf("campaign_id")
Dim selectedActivity As String
selectedActivity = primary.StringOf("campaign_activity_id")
If ((selectedCampaign = "") Or (selectedActivity = "")) Then
    Debug.Print "Missing campaign or activity ID."
    Exit Sub
End If

Debug.Print "Campaign ID: " & selectedCampaign
Debug.Print "Primary activity ID: " & selectedActivity
Dim selection As New ChilkatJsonObject
success = selection.UpdateString("name",campaign.StringOf("name"))
success = selection.UpdateString("campaign_id",selectedCampaign)
success = selection.UpdateString("campaign_activity_id",selectedActivity)
success = selection.WriteFile("newsletter-selection.json")
If (success <> 1) Then
    Debug.Print "Could not save newsletter-selection.json. Keep the IDs printed above."
    Exit Sub
End If

Debug.Print "Saved newsletter-selection.json."