VBScript Requires Chilkat v11.0.0+
VBScript
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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault = CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
set tokens = CreateObject("Chilkat.JsonObject")
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
outFile.WriteLine("Run the desktop authorization example using this account and store.")
WScript.Quit
End If
If (tokens.JsonTypeOf("access_token") <> 1) Then
outFile.WriteLine("The saved authorization has no access token.")
WScript.Quit
End If
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = tokens.StringOf("access_token")
set resp = CreateObject("Chilkat.HttpResponse")
campaignName = "Workshop newsletter"
campaignId = ""
set url = CreateObject("Chilkat.StringBuilder")
success = url.Append("https://api.cc.email/v3/emails?limit=50")
set page = CreateObject("Chilkat.JsonObject")
Do While ((url.Length > 0) And (campaignId = ""))
success = http.HttpNoBody("GET",url.GetAsString(),resp)
If ((success <> 1) And (resp.StatusCode <= 0)) Then
outFile.WriteLine("Read campaigns: HTTP request failed.")
WScript.Quit
End If
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
outFile.WriteLine("Read campaigns: HTTP " & resp.StatusCode & " " & resp.BodyStr)
WScript.Quit
End If
success = page.Load(resp.BodyStr)
count = page.SizeOfArray("campaigns")
If ((success <> 1) Or (count < 0)) Then
outFile.WriteLine("Expected a page of campaigns.")
WScript.Quit
End If
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
outFile.WriteLine("Unexpected campaign pagination link.")
WScript.Quit
End If
success = url.Prepend("https://api.cc.email")
End If
Loop
If (campaignId = "") Then
outFile.WriteLine("No campaign named " & campaignName)
WScript.Quit
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
outFile.WriteLine("Read campaign: HTTP request failed.")
WScript.Quit
End If
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
outFile.WriteLine("Read campaign: HTTP " & resp.StatusCode & " " & resp.BodyStr)
WScript.Quit
End If
set campaign = CreateObject("Chilkat.JsonObject")
success = campaign.Load(resp.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a JSON object in the response.")
WScript.Quit
End If
' The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
set primary = CreateObject("Chilkat.JsonObject")
success = campaign.FindRecord2("campaign_activities","role","primary_email",1,primary)
If ((success <> 1) Or (primary.JsonTypeOf("campaign_activity_id") <> 1)) Then
outFile.WriteLine("No primary email activity ID in the response.")
WScript.Quit
End If
selectedCampaign = campaign.StringOf("campaign_id")
selectedActivity = primary.StringOf("campaign_activity_id")
If ((selectedCampaign = "") Or (selectedActivity = "")) Then
outFile.WriteLine("Missing campaign or activity ID.")
WScript.Quit
End If
outFile.WriteLine("Campaign ID: " & selectedCampaign)
outFile.WriteLine("Primary activity ID: " & selectedActivity)
set selection = CreateObject("Chilkat.JsonObject")
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
outFile.WriteLine("Could not save newsletter-selection.json. Keep the IDs printed above.")
WScript.Quit
End If
outFile.WriteLine("Saved newsletter-selection.json.")
outFile.Close