PowerShell Requires Chilkat v11.0.0+
PowerShell
Constant Contact V3: Restore a deleted contact to a list
See more Constant Contact Examples
Look up the deleted contact by email, then restore it by adding its existing ID to a chosen list. Constant Contact documents this membership activity as a restoration method. It changes only membership, preserves the saved profile, and makes no email-permission claim. Select the intended destination list explicitly; previous memberships were removed by deletion. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat PowerShell Downloads
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
$query = New-Object Chilkat.HttpRequest
$query.HttpVerb = "GET"
$query.AddParam("email","reader+newsletter@example.com")
$query.AddParam("status","deleted")
$success = $http.HttpReq("https://api.cc.email/v3/contacts",$query,$resp)
if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
$("Expected HTTP 200. Received " + $resp.StatusCode)
if ($resp.StatusCode -eq 401) {
$("Refresh the saved access token or authorize again.")
}
exit
}
$matches = New-Object Chilkat.JsonObject
$success = $matches.Load($resp.BodyStr)
if ($success -ne $true) {
$("Expected a JSON response.")
exit
}
if (($matches.SizeOfArray("contacts") -ne 1) -or ($matches.JsonTypeOf("contacts[0].deleted_at") -ne 1)) {
$("Expected one deleted contact for this email. No restoration submitted.")
exit
}
if (($matches.JsonTypeOf("contacts[0].contact_id") -ne 1) -or ($matches.StringOfEquals("contacts[0].contact_id","",$true) -eq $true)) {
$("Missing contacts[0].contact_id in the response.")
exit
}
$body = New-Object Chilkat.JsonObject
$body.UpdateString("source.contact_ids[0]",$matches.StringOf("contacts[0].contact_id"))
$body.UpdateString("list_ids[0]","DESTINATION_LIST_ID")
$success = $http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",$body,"application/json",$resp)
if (($success -ne $true) -or ($resp.StatusCode -lt 200) -or ($resp.StatusCode -ge 300)) {
$("Could not start the membership activity. HTTP " + $resp.StatusCode)
exit
}
$job = New-Object Chilkat.JsonObject
$success = $job.Load($resp.BodyStr)
if ($success -ne $true) {
$("Expected a JSON response.")
exit
}
if (($job.JsonTypeOf("activity_id") -ne 1) -or ($job.StringOfEquals("activity_id","",$true) -eq $true)) {
$("Missing activity_id in the response.")
exit
}
$activityId = $job.StringOf("activity_id")
$("Submitted activity ID: " + $activityId)
$http.SetUrlVar("job",$activityId)
# Poll briefly. A longer-running job can be checked later using the same ID.
$attempt = 0
while (($attempt -lt 5)) {
$success = $http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",$resp)
if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
$("Status check returned HTTP " + $resp.StatusCode + ". Keep the activity ID and check again later.")
exit
}
$success = $job.Load($resp.BodyStr)
if (($success -ne $true) -or ($job.JsonTypeOf("state") -ne 1)) {
$("Missing activity state. Keep the activity ID.")
exit
}
$state = $job.StringOf("state")
if (($state -ne "initialized") -and ($state -ne "processing")) {
$("Activity state: " + $state)
$i = 0
while (($i -lt $job.SizeOfArray("activity_errors"))) {
$job.I = $i
$($job.StringOf("activity_errors[i]"))
$i = $i + 1
}
exit
}
$attempt = $attempt + 1
if ($attempt -lt 5) {
$http.SleepMs(2000)
}
}
$("Still processing. Recheck the activity ID before submitting another request.")