Search Results based on User Roles

Aden Tutton asked on November 28, 2017 14:43

Hi All,

I have a group of search results which currently gather pages from a number of categories branching off a parent page (breeds of animal). What I would like to do is to filter the search results based on the aubscription a user has, so if they only have one type of subscription then only those results would show in the search results. I have split the smart search results into separate search indexes but an struggling to find the logic to assign the correct index to the search results based on the user role

Below is my current code in the indexes field of my web part


MembershipCodeNames = "AnnualSubscriptionRole" + suffix;
TrialMembershipCodeNames = "TrialSubscriptionRole" + suffix;

if (MembershipContext.AuthenticatedUser.HasMembership(MembershipCodeNames) || MembershipContext.AuthenticatedUser.HasMembership(TrialMembershipCodeNames)) {
    index = suffix + "Results";



Recent Answers

Dragoljub Ilic answered on November 28, 2017 15:04

Hi Aden,

If I understand you well, you want to choose correct index based on current user role? If that is the case, you can use this piece of code to check if current user is in specific role: MembershipContext.AuthenticatedUser.IsInRole("YourRoleName", SiteContext.CurrentSiteName)

I think that this, in combination with simple if-else statement will be enough to solve your problem.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on November 28, 2017 16:09 (last edited on March 24, 2018 12:16)

Are you using portal model or ACX? If i understand correctly you want to use a chosen index for the smart search results web part. You can try this approach:

MembershipCodeNames = "AnnualSubscriptionRole" + suffix;
TrialMembershipCodeNames = "TrialSubscriptionRole" + suffix;
seachIndex = "defaultsearchIndex"; // some default index if there no trial or other subscription
if(MembershipContext.AuthenticatedUser.HasMembership(MembershipCodeNames)) {seachIndex ="membershipSearchIndexName"}
if(MembershipContext.AuthenticatedUser.HasMembership(TrialMembershipCodeNames)) {seachIndex ="trialSearchIndexName"}
return seachIndex;

But first of all make sure that you macro returns something: {% MembershipContext.AuthenticatedUser.Memberships.Count %}

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on November 28, 2017 18:05 (last edited on March 24, 2018 12:16)

Diregard my previous post. I've taken some time to investigate the issue. I know It is kind of crazy. You probably should read more on Membership management

I've checked this on the version 9. For some reason you can find your assignments in OtherBindings, i.e. {% CurrentUser.OtherBindings.MembershipUsers%} . We are talking here about simple membership (no products and roles involved) Noramlly you have 3 tables involved:

select * from CMS_Membership
select * from CMS_MembershipRole
select * from CMS_MembershipUser

I our case you need only 2: CMS_Membership, CMS_MembershipUser, for example: select * from CMS_Membership where MembershipSiteID =5 will get your the memberships assigned to your site if your siteid = 5

On my site I have 2 memberships:

MembershipID    MembershipName  MembershipDisplayName
    3           GoldPartners    Gold Partners
    4           SilverPartners  Silver Partners

You probably have to operate with MembershipID here, so to check if your user has it - to do this {% CurrentUser.OtherBindings.MembershipUsers.Where("MembershipID =3").Count > 0%}

If you want to see the membership name it is kinda crazy but if count > 0 then

CurrentUser.OtherBindings.MembershipUsers.Where("MembershipID =3").FirstItem.Parent.DisplayName

So to rewrite the macro, find out your MemerbshipIDs:

AnnualSubscriptionMembershipID =  123;  // see select * from CMS_Membership 
TrialMembershipID = 456; //
seachIndex = "defaultsearchIndex"; // some default index if there no trial or other subscription
if(CurrentUser.OtherBindings.MembershipUsers.Where("MembershipID =" + AnnualSubscriptionMembershipID ).Count > 0) {seachIndex ="membershipSearchIndexName"}
if(CurrentUser.OtherBindings.MembershipUsers.Where("MembershipID =" + TrialMembershipID ).Count > 0) {seachIndex ="trialSearchIndexName"}
return seachIndex;

P.S. I am not quite sure if you are talking about roles or membership, but for roles you can use the same approach, to see your role (you might have many, this is just thte first one in the list): {% CurrentUser.OtherBindings.UserRoles[0].Parent.CodeName %}

1 votesVote for this answer Mark as a Correct answer

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