How to show newsletter(s) to subscribe in dependence on user´s role

   —   
This article describes how to show newsletter(s) to subscribe in dependence on user´s role.
Let say you have two kinds of newsletter issues (business and non-business) and two roles, for business partners and for others. Current newsletter subscription web part shows all of them. If you want to show business issues for business role and non-business for other role, you can use following approach.
You can modify /CMSWebParts/Membership/NewsletterSubscriptionWebPart.ascx.cs file.
Let´s say that you have two newsletters. Newsletter1 with ID=1 and Newsletter2 with ID=2 (these IDs can be found in database, in Newsletter_Newsletter table). You have also two roles: RoleA and RoleB.
And you want to achieve this scenario. If a current user is in RoleA, you want to show him Newsletter1 only. For user in RoleB you want Newsletter2 to be shown and by default, you want to show all newsletter.

Then, you can ensure it using this code (between lines 390 - 408, version 4.1):

// ds = NewsletterProvider.GetAllNewsletersForSite(CMSContext.CurrentSiteID);

if (CMS.CMSHelper.CMSContext.CurrentUser.IsInRole("NewsletterRole1", CMS.CMSHelper.CMSContext.CurrentSiteName))
{

ds = NewsletterProvider.GetNewsleters("NewsletterId = '1'",null); // .GetNewsletter(1);

}

else if (CMS.CMSHelper.CMSContext.CurrentUser.IsInRole("NewsletterRole2", CMS.CMSHelper.CMSContext.CurrentSiteName))
{

ds = NewsletterProvider.GetNewsleters("NewsletterId = '2'",null);

}
else
ds = NewsletterProvider.GetAllNewsletersForSite(CMSContext.CurrentSite.SiteID);



See also: Newsletters module

Applies to: Kentico CMS 4.x
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.