This post is actually self serving, in that I will never have to hunt for it again. First, you of course have to install the MailingList Module and then Add a Reference to the Sitecore.MailingList.dll in the bin folder. Then add:
using Sitecore.Modules.MailingList.Core;
It needs some parameter validation before being 100%, but here goes:
private void SaveSubscriber(string firstName, string lastName, string email, string gender, string age)
{
MailingList list = new MailingList();
string listID = Sitecore.Context.Database.Items.GetItem("/sitecore/content/modules/mailing list/mailing lists/Newsletter").ID.ToString();
string name = firstName + " " + lastName;
string company = "";
string country = Sitecore.Context.Language.ToString();
list.PutSubscriber(name, email, company, country);
// listID parameter says it’s listID’s although it’s not a string array, beats me
list.Subscribe(name, email, listID);
// you can send any parameters here that you want
list.PutSubscriberField(email, "Gender", gender);
list.PutSubscriberField(email, "Age", age);
}
private void RemoveSubscriber(string email)
{
MailingList list = new MailingList();
string listID = Sitecore.Context.Database.Items.GetItem("/sitecore/content/modules/mailing list/mailing lists/Newsletter").ID.ToString();
// Second parameter expects string array of Newsletter Sitecode Id’s to subscribe to.
list.Unsubscribe(email, listID.Split(‘,’));
}
Let me know if you find it useful or know of something I was overlooking. Most of this was taking from Alex’s post and just updated to Sitecore CMS version 5.3.
