C# Wrapper Bulk Add/Update Contacts

Regular Participant

C# Wrapper Bulk Add/Update Contacts

Am I missing a method in the C# wrapper that can bulk add/update contacts but I'm not seeing anything in the wrapper for that. Does it exist?

 

My plan is to do a bulk add/update to do the initial put, then my understanding is I have to then loop through again to get the ID's since the bulk operation doesn't return them. Then on updates, recall them by ID and update them. Is this the correct way to go? This is all happening in a Windows service, not ASP.net.

 

Thanks,

 

13 REPLIES 13
Employee

Hi,

 

There is a bulk upload method in the wrapper.  You can find it in Utility.cs under bulkUrlEncoded (or do a search for "bulk"  in that file).

 

I wouldn't loop back through to get ID's once your contacts are added; I would do a search for contact by email address to find the ID of the contact you'd like to update and then update appropriately whenever you need to update a particular contact.  You are correct that we don't return the contact ID's for bulk actions through the API.

 

I don't have an example for the bulk upload, but to update an existing contact you could do something like below.  Let me know if you'd like my C# script that adds contacts individually or updates them if they already exist (new contacts are handled differently than existing contacts).  You should use the bulk upload method if you'd like to upload/update a large number of contacts at once.

 

//Contact update example
string _nextChunk;
string emailAddress = "test_01242013@test.com";
List<string> email = new List<string>();
email.Add(emailAddress);
IList<Contact> myContact = Utility.SearchContactByEmail(authdata, email, out _nextChunk);

//Get Contact by ID
Contact contact = Utility.GetContactDetailsById(authdata, myContact[0].Id);
contact.EmailAddress = emailAddress;

//Add Lists
ContactOptInList newList = new ContactOptInList();
newList.ContactList = new ContactList("1"); //Contact list you want to add them to
contact.ContactLists.Add(newList);
contact.OptInSource = ContactOptSource.ActionByContact; //set to ActionByCustomer if you'd like to suppress email notifications

//Add contact properties
contact.FirstName = "Joe";
contact.LastName = "Test";

//Update contact
Utility.UpdateContactFullForm(authdata, contact);

 

 

 

Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Thanks Shannon,

 

So each time I want to update to contact I should recall them by their email, then make my changes, then post again using their ID? In that case do I even need to save their ID locally for anything?


And yes, I'd love to see any examples you have. Thank you.

Exactly right, I wouldn't store the ID's locally on your side, just query by email address to do the update.  Here is my script to add a contact if not already in the system, or update it if it is.  It is basically the functionality for a signup form (on a website), but it sounds like you want to do a bulk upload then just updates as needed.

 

/*
//Contact add/update example
string _nextChunk;
string emailAddress = "test_01242013@test.com";
List<string> email = new List<string>();
email.Add(emailAddress);
IList<Contact> myContact = Utility.SearchContactByEmail(authdata, email, out _nextChunk);
				
if (myContact.Count == 0)
{//Contact Does not exist, create new contact

	Contact contact = new Contact();
	contact.EmailAddress = emailAddress;
		
	//Add Lists
	ContactOptInList newList = new ContactOptInList();
	newList.ContactList = new ContactList("1"); //Contact list you want to add them to
	contact.ContactLists.Add(newList);

	//Add contact properties
	contact.FirstName = "Joe";
	contact.LastName = "Test";
	contact.OptInSource = ContactOptSource.ActionByContact;

	//Add contact
	Utility.CreateNewContact(authdata, contact);
}

else
{//Contact does exist,  update existing
	//Get Contact by ID
	Contact contact = Utility.GetContactDetailsById(authdata, myContact[0].Id);
	contact.EmailAddress = emailAddress;

	//Add Lists
	ContactOptInList newList = new ContactOptInList();
	newList.ContactList = new ContactList("1"); //Contact list you want to add them to
	contact.ContactLists.Add(newList);
	contact.OptInSource = ContactOptSource.ActionByContact;

	//Add contact properties
	contact.FirstName = "Joe";
	contact.LastName = "Test";
	   
	//Update contact
	Utility.UpdateContactFullForm(authdata, contact);
}

 

 

Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Thanks again, Shannon. That was pretty much how I envisioned it.

 

What is the recommended approach for bulk updates? If there are more than x posts to perform then use bulk, otherwise use invidivual? What would X be? Or should it just be if there are more than 1 insert/update to perform, use bulk?

Hi,

 

I would recommend using the bulk uploader for anything more than 1 contact.  By the time you get to updating more than 5 contacts at a time individually, you can start to see a script fail.

 

Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Thanks for the fast replies, Shannon. Appreciate it! I'll let you know how it goes!

Regular Participant

bulkUrlEncoded is a mess in my opinion. You should write a new method in your wrapper that allows you to take an ILIst<Contact> and deal with all of these field mappings for you.

Regular Participant

Also, if I'm just going to be using bulk post for every call, do I even need to search for contacts by email first? Or will the bulk do updates/inserts where appropriate without me needing to worry about it?

Occasional Participant

One more, can I set do not mail from bulk?

 

Say I go through and add everyone to list, then in my system they're removed from mailing list, I want to remove them from CC. How can I do that with bulkUrlEncoded?

Occasional Participant

Ooops, I'm the same person, just logged in with one of my test accounts :)

Hi,

 

The bulk feature does work differently than the rest of our APIs.  You don't need to search by email if you use the bulk API, because new contacts are added and existing contacts are updated as appropriate.  You would only need to search first if you're updating contacts individually NOT using the bulk API.

 

The bulk API doesn't have a way to set contacts to Do Not Mail--you would have to do that on an individual basis.  You'd probably want to build a form that handles individually opted out contacts separately from the bulk added/updated contacts.  

 

Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Thanks for the reply. Well, I don't have a form. This is being integrated as a Windows Service connecting to a database. It looks for people to insert/update and remove from mailing list to get the DB up to date with Constant Contact.

 

If there are a bunch of removals (not anticipated, but planning for all scenarios) and I handle them one by one am I going to start to see errors? Can I put in a feature request to have bulk update take in the DNM field?

Yes, I will forward on your feature request to my product team. The worry with something like that might have been that a large number of contacts could be put erroneously onto the Do Not Mail list (from which they are hard to get off), but we do have a similar feature in the UI to move an entire contact list to Do Not Mail, so I don't see why this couldn't be implemented.

 

Depending on how quickly your script runs, you could see some errors if you try to move a lot of contacts to Do Not Mail at once. You could build in a slight delay to prevent this. You could write your script to move 1 contact to Do Not Mail, delay 300ms, move the next, delay 300ms, etc, storing the email addresses to be moved until the job is complete. Sorry for the workaround.

 

EDIT: You shouldn't need to build in a delay for DELETE calls; our server handles those so quickly that it should never be an issue.

 

Best Regards,
Shannon W.
API Support Specialist

Developer Portal

View API documentation, code samples, get your API key.

Visit Page