Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Web Part Display To Roles View modes: 
User avatar
Member
Member
kiat-earth9 - 8/22/2012 4:43:26 AM
   
Web Part Display To Roles
Hello, i need some help on this. I have a webpart that i want to display to certain roles on certain pages. i know in properties for web parts, there is a display role and visiblity and i can add macros to it.

I'm using a template with a certain webpart. Page a,b,c,d,e,f are using this webpart.

- I want to set it such that at page a,b,c. the webpart is displayed for role editor, whereas for page d,e. I want to hide the webpart for role editor (using the same template)
- Then for page a,b,c, the webpart is hidden from role editorB, whereas for page d,e. I want to show it for role editorB.


Can anyone help me or give me an example of the coding because i have no idea how to do it. thanks

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 8/23/2012 5:55:45 AM
   
RE:Web Part Display To Roles
Hello,

Well, the easiest solution is to add the web part to the template twice, one with the visibility for role "editor" and another one for "editorB"
Then we would recommend to clone the page template and swap the roles in the web parts according to your needs and use the alternative page template for the other part of the pages.

Would that be feasible in your scenario? You can't loose anything by cloning a template.
Only disadvantage would be that every design change would have to be made in two templates... or you could, again, clone the template and just swap the roles visibility in the web parts...

Hope it makes sense.

Regards,
Zdenek

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 10:45:18 AM
   
RE:Web Part Display To Roles
On this note, I have found that, in some scenarios, I've needed to hide a webpart for certain roles while showing it for others. I would like to be able to tell the web part to do this:

Show this webpart for authenticated users, except for when they are in this role.

An example scenario is a web part that contains a form to register to be assigned to a special role, but only if you are already a registered and authenticated user.

It would be nice to have 2 fields for role visibility. One for roles to show the webpart to and one for roles to hide the webpart from.

Right now I am thinking of subclassing CMSAbstractWebPart to include an automatic check for a field named "HideFromRoles" and override OnContentLoaded to check this value and stop processing and hide the webpart appropriately. Then I should be able to change the base class on any web part for which I wish to implement this feature (or clone the webpart and use the clone instead for upgrade reasons)

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 11:00:21 AM
   
RE:Web Part Display To Roles
Here is the subclass I made:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;

namespace CMS.PortalControls
{
public partial class CustomCMSWebPart : CMSAbstractWebPart
{
public string HideForRoles
{
get
{
return ValidationHelper.GetString(this.GetValue("HideForRoles"), string.Empty);
}
set
{
this.SetValue("HideForRoles", value);
}
}
public override void OnContentLoaded()
{
base.OnContentLoaded();
}
public override bool HideOnCurrentPage
{
get
{
if (!base.HideOnCurrentPage)
{
if (this.HideForRoles.Trim().Trim(new char[] { ';' }) != "")
{
CurrentUserInfo currentUser = CMSContext.CurrentUser;
if (currentUser.IsGlobalAdministrator)
{
return false;
}
bool flag = false;
foreach (string str in this.HideForRoles.Split(new char[] { ';' }))
{
if (currentUser.IsInRole(str, this.CurrentSiteName))
{
flag = true;
break;
}
}
return flag;
}
return false;
}
else
{
return true;
}

}
}
public CustomCMSWebPart():base()
{

}
}
}


User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/24/2012 11:44:42 AM
   
RE:Web Part Display To Roles
Here is an example of a K# macro you can use in the ShowToRoles field:


{%if(Contains("|2454|1245|1454|","|" + NodeID +"|") == true)
{
return "Editor"
}
else
{
if(Contains("|2454|1245|1454|","|"+ NodeID + "|") == true)
{
return "EditorB"
}
else
{
return "Editor;EditorB"
}
}
%}



Apparently K# doesn't like "else if" so I had to nest the second condition.

I am using the NodeIDs because it is much easier to code than something like the NodeAliasPath or NodeGuid, however you could use any other condition that is easy for you to check and works for your scenario. For example, if all of the pages you wanted to show the webpart for EditorB on were under a specific path, you could use this expression in your condition "StartsWith(NodeAliasPath,"/A/ParentPath/")"

If you feel that it is easier for you to just edit this web part's properties to add node ids to the K# macro than to mess around with cloning page templates, then use this method. Otherwise use the method suggested by Zdenek.

If it were me, and I didn't have to make it so other people could control the visibility of these webparts, I would use the K# macro.