Custom Unsubscribe form widget
When a user Unsubscribes, we want them to be removed from ALL mailing lists. To do this, do I need to create a custom unsubscribe form widget?
Sitefinity 4.2
Hi Laura,
Yes, the Unsubscribe Form control works with just one mailing list. To be able to unsubscribe from all mailing lists you may inherit it. Your new control should be something like this:
public
class
CustomUnsubscribe : UnsubscribeForm
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
base
.InitializeControls(container);
this
.UnsubscribeButton.Click -=
this
.UnsubscribeButton_Click;
this
.UnsubscribeButton.Click +=
this
.UnsubscribeButtonCustom_Click;
protected
void
UnsubscribeButtonCustom_Click(
object
sender, EventArgs e)
this
.RemoveSubscriber();
private
void
RemoveSubscriber()
var newslettersManager = NewslettersManager.GetManager(
this
.ProviderName);
var email =
this
.EmailAddress.Text.ToLower();
var subscriber = newslettersManager.GetSubscribers().Where(s => s.Email == email).SingleOrDefault();
if
(subscriber !=
null
)
var mailLists = newslettersManager.GetMailingLists().Where(m => m.Subscribers.Contains(subscriber));
foreach
(var mailList
in
mailLists)
newslettersManager.Unsubscribe(subscriber, mailList.Id);
newslettersManager.SaveChanges();