Sitecore MailingList Module

I recently had the opportunity to work with the Sitecore 5.3 MailingList Module and since it was my first time, I had to hunt around the API to figure out how to have a site visitor subscribe and unsubscribe from a MailingList. The only thing I could find was this post by Alex Shyba from back in 2005 when I guess it was actually called the Newsletter module.

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.

Technorati Tags: ,,,

Leave a Reply

Your email address will not be published. Required fields are marked *