I customized the custom registration form webpart in Kentico and added this code to the code behind:
//fetch field in my registration form for determining if user is signed up for newsletter or not
EditingFormControl chkSubscribeNews = formUser.FieldEditingControls["SubscribeNewsLetter"] as EditingFormControl;
if (chkSubscribeNews != null)
{
bool subscribeNews = false;
subscribeNews = ValidationHelper.GetBoolean(chkSubscribeNews.Value, false);
if (subscribeNews)
{
//check to see if email is already assigned to newsletter...if so, then skip adding again
SubscriberInfo currentSubscriber = SubscriberInfoProvider.GetSubscriberInfo("cms.user", ui.UserID, SiteContext.CurrentSiteID);
if (currentSubscriber == null)
{
//add check to see if email exists for subscriber
//subscribe user to newsletter
// Create new subscriber object
SubscriberInfo newSubscriber = new SubscriberInfo();
// Set the properties
newSubscriber.SubscriberFirstName = ui.FirstName;
newSubscriber.SubscriberLastName = ui.LastName;
newSubscriber.SubscriberFullName = ui.FullName;
newSubscriber.SubscriberRelatedID = ui.UserID;
newSubscriber.SubscriberType = "cms.user";
newSubscriber.SubscriberSiteID = SiteContext.CurrentSiteID;
// Save the subscriber
SubscriberInfoProvider.SetSubscriberInfo(newSubscriber);
//refetch user subscriber to make sure it was filled and no errors occurred
SubscriberInfo subscriber = SubscriberInfoProvider.GetSubscriberInfo(newSubscriber.SubscriberID);
NewsletterInfo newsletter = NewsletterInfoProvider.GetNewsletterInfo("MyNewsletter", SiteContext.CurrentSiteID);
if ((subscriber != null) && (newsletter != null))
{
// Subscribe to 'My new static newsletter'
try
{
SubscriberInfoProvider.Subscribe(subscriber.SubscriberID, newsletter.NewsletterID, DateTime.Now);
}
catch (Exception ex)
{
EventLogProvider.LogException("CustomSubscription", "ERROR", ex);
}
}
}//End currentSubscriber null check
}
}