Email marketing, subscribe contacts with code behind

Kenny Deblaere asked on July 1, 2016 13:36

Good afternoon

I want to subscribe a contact to my newsletter marketing campaign. In the Kentico CMS, it's quite easy, but I want to do it like following link. I check if the given e-mail is already in my contact list and I want to add the contact to the subscribers of the campaign.

I hope this explains my problem.

Kind regards

Kenny

Correct Answer

Rui Wang answered on July 1, 2016 16:23

You can find the code for subscribe a Contact to newsletter in \CMS\CMSModules\Newsletters\Tools\Newsletters\Newsletter_Subscribers.aspx.cs Essentially, you are passing the contact info in to a subscriber info.

            // Get contact's info
            DataSet contactData = ModuleCommands.OnlineMarketingGetContactForNewsletters(contactID, "ContactFirstName,ContactMiddleName,ContactLastName,ContactEmail");
            if (DataHelper.DataSourceIsEmpty(contactData))
            {
                continue;
            }

            string firstName = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactFirstName"], string.Empty);
            string lastName = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactLastName"], string.Empty);
            string middleName = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactMiddleName"], string.Empty);
            string email = ValidationHelper.GetString(contactData.Tables[0].Rows[0]["ContactEmail"], string.Empty);

            // Create new subscriber of contact type
            subscriber = new SubscriberInfo();
            subscriber.SubscriberFirstName = firstName;
            subscriber.SubscriberLastName = lastName;
            subscriber.SubscriberEmail = email;
            subscriber.SubscriberFullName = new SubscriberFullNameFormater().GetContactSubscriberName(firstName, middleName, lastName);
            subscriber.SubscriberSiteID = siteId;
            subscriber.SubscriberType = PredefinedObjectType.CONTACT;
            subscriber.SubscriberRelatedID = contactID;

            CheckPermissionsForSubscriber(subscriber);

            SubscriberInfoProvider.SetSubscriberInfo(subscriber);
0 votesVote for this answer Unmark Correct answer

Recent Answers


Virgil Carroll answered on July 1, 2016 16:24

Kenny,

I am not really sure what you are asking? You shared the link to how to do code behind to create a subscriber. Are you looking for help on how you would actually implement this? Or are you looking for help on how to change the code to validate that an email is not already a subscriber?

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on July 1, 2016 16:27

Virgil, I think Kenny is not sure about how to link a Contact to a Subscriber as the example is for adding a subscriber directly.

2 votesVote for this answer Mark as a Correct answer

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