C# Requires Chilkat v11.0.0+
C#
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat C# Downloads
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Chilkat.Secrets vault = new Chilkat.Secrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
Chilkat.JsonObject entry = new Chilkat.JsonObject();
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
Chilkat.JsonObject tokens = new Chilkat.JsonObject();
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
Debug.WriteLine("Run the desktop authorization example using this account and store.");
return;
}
if (tokens.JsonTypeOf("access_token") != 1) {
Debug.WriteLine("The saved authorization has no access token.");
return;
}
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.AuthToken = tokens.StringOf("access_token");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
// Use contact IDs from email lookups and the list ID from the selected destination.
Chilkat.JsonObject body = new Chilkat.JsonObject();
body.UpdateString("source.contact_ids[0]","FIRST_CONTACT_ID");
body.UpdateString("source.contact_ids[1]","SECOND_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 != true) || (resp.StatusCode < 200) || (resp.StatusCode >= 300)) {
Debug.WriteLine("Could not start the membership activity. HTTP " + Convert.ToString(resp.StatusCode));
return;
}
Chilkat.JsonObject job = new Chilkat.JsonObject();
success = job.Load(resp.BodyStr);
if (success != true) {
Debug.WriteLine("Expected a JSON response.");
return;
}
if ((job.JsonTypeOf("activity_id") != 1) || (job.StringOfEquals("activity_id","",true) == true)) {
Debug.WriteLine("Missing activity_id in the response.");
return;
}
string activityId = job.StringOf("activity_id");
Debug.WriteLine("Submitted activity ID: " + activityId);
http.SetUrlVar("job",activityId);
// Poll briefly. A longer-running job can be checked later using the same ID.
int attempt = 0;
while ((attempt < 5)) {
success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp);
if ((success != true) || (resp.StatusCode != 200)) {
Debug.WriteLine("Status check returned HTTP " + Convert.ToString(resp.StatusCode) + ". Keep the activity ID and check again later.");
return;
}
success = job.Load(resp.BodyStr);
if ((success != true) || (job.JsonTypeOf("state") != 1)) {
Debug.WriteLine("Missing activity state. Keep the activity ID.");
return;
}
string state = job.StringOf("state");
if ((state != "initialized") && (state != "processing")) {
Debug.WriteLine("Activity state: " + state);
int i = 0;
while ((i < job.SizeOfArray("activity_errors"))) {
job.I = i;
Debug.WriteLine(job.StringOf("activity_errors[i]"));
i = i + 1;
}
return;
}
attempt = attempt + 1;
if (attempt < 5) {
http.SleepMs(2000);
}
}
Debug.WriteLine("Still processing. Recheck the activity ID before submitting another request.");