User auto-registration on subscribing the newsletter

Aashish Khandelwal asked on November 17, 2014 18:53

How can I configure the existing system so that when a user subscribes to a newsletter, he must be registered automatically?

Correct Answer

David Komárek answered on November 21, 2014 14:43

Hi Aashish,

indeed, as Joshua suggests, you may combine the two web parts (Registration form and Newsletter subscription) or their variants and include all the necessary fields and registration logic. However, I believe that the easier approach would actually be the first method - extending the (custom) Registration form as the registration process is much more complicated to add, compared to simply subscribing a user to a newsletter as a subscriber.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Joshua Adams answered on November 17, 2014 20:27

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

                //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'
                        SubscriberInfoProvider.Subscribe(subscriber.SubscriberID, newsletter.NewsletterID, DateTime.Now);
                    catch (Exception ex)
                        EventLogProvider.LogException("CustomSubscription", "ERROR", ex);
            }//End currentSubscriber null check
0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on November 17, 2014 20:29

If you decide to use this approach, then make sure you clone the custom registration form webpart instead of making customizations directly on the webpart. This is a kentico best practice and will help with upgrades.

1 votesVote for this answer Mark as a Correct answer

Aashish Khandelwal answered on November 19, 2014 16:27

Hi Joshua,

This is not working.

I want to implement Auto registration part when someone subscribes to a newsletter with his details like Name and email address.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on November 19, 2014 22:39

So you want for someone to be registered as a user when they subscribe to a newsletter? If thats the case, you will have to customize the newsletter subscription webpart to have more fields and custom code on the submission to create a user. You may want to check on the registration form webpart, to see what the minimal set of fields is required for a user to be created and start from there.

If thats not the case, you may need to elaborate.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.