Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Anders Hattestad
Jun 30, 2011
  4454
(0 votes)

How to make Property LinkCollection use Visitor Groups

I have started to look some more into how the visitor groups are handled in EPiServer. It’s a function that allows parts of a text inside the editor to only be displayed for some user groups.

This function can be used other places also. For instance I wanted to make a LinkCollection where I could set for each link a visitor group.

I found one blog post by Paul Smith that show how we check if a user is in a visitor group.

But first I had to extend the normal PropertyLinkCollection with an extra column for visitor groups.

To archive this I had to copy the user control that is used by the Link Collection property.

image

And I had to reverse engineer the popup function from the editor so I could set and retrieve the selected visitor groups. basicly its only to open a dialog like this

Code Snippet
  1. EPi.CreateDialog(personalizedEditorUrl+"?groups="+groups+"&contentGroup="+contentGroup, onDialogComplete, onCompleteArguments , dialogArguments, {width: 460, height: 450, scrollbars:"no"});

where the url is

UriSupport.ResolveUrlFromUIBySettings("Editor/Dialogs/PersonalizedContent.aspx")

 

image

Then I needed to add these new attributes to the links. I just added them to the LinkItem as an attribute, so the new attributes will be added to the links link this:

image

In edit mode I then created a method that took an LinkItem as an argument and found out all the visitor groups it contains

Code Snippet
  1. {
  2.     string result = "";
  3.     string result2 = "";
  4.     var groups = GetAttribute(item, "data-groups");
  5.     if (!string.IsNullOrEmpty(groups))
  6.     {
  7.         var store = new VisitorGroupStore();
  8.         foreach (var id in groups.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  9.             result += "<div class='epi_vg'>" +  store.Load(new Guid(id)).Name + "</div>";
  10.         if (result != "")
  11.             result = "<div class='epi_pc_l'>" + result + "</div>";
  12.     }
  13.     var contentgroup = GetAttribute(item, "data-contentgroups");
  14.     if (!string.IsNullOrEmpty(contentgroup))
  15.     {
  16.         foreach (var id in contentgroup.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  17.             result2 += "<div class='epi_lg'><span>" + id + "</span></div>";
  18.         if (result2 != "")
  19.             result2 = "<div class='epi_pc_t'>" + result2 + "</div>";
  20.     }
  21.     return result + result2;
  22. }
Then I needed to filter the normal view of this Property, so I hide those links I’m not allowed to see. as far as I can understand this concept the check could be done like this. But if anyone have any pointers to if this is wrong, or should be done in another way please let me know.

Code Snippet
  1. public override void CreateDefaultControls()
  2. {
  3.     this.CopyWebAttributes(this);
  4.     HtmlGenericControl htmlGenericControl = new HtmlGenericControl("ul");
  5.     if (!string.IsNullOrEmpty(this.CssClass))
  6.         htmlGenericControl.Attributes.Add("class", this.CssClass);
  7.     this.Controls.Add(htmlGenericControl);
  8.     var helper = new VisitorGroupHelper();
  9.     var store = new VisitorGroupStore();
  10.     foreach (LinkItem current in this.PropertyLinkCollection)
  11.     {
  12.         var groups = GetAttribute(current, "data-groups");
  13.         var show = true;
  14.         if (!string.IsNullOrEmpty(groups))
  15.         {
  16.             show = false;
  17.             foreach (string id in groups.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  18.             {
  19.                 var group = store.Load(new Guid(id));
  20.                 if (helper.IsPrincipalInGroup(EPiServer.Security.PrincipalInfo.CurrentPrincipal, group.Name))
  21.                     show = true;
  22.             }
  23.         }
  24.         try
  25.         {
  26.             var url = new UrlBuilder(current.Href);
  27.             bool isEPiServerPage = PermanentLinkMapStore.ToMapped(url);
  28.             if (isEPiServerPage)
  29.             {
  30.                 var page = DataFactory.Instance.GetPage(PermanentLinkUtility.GetPageReference(url));
  31.  
  32.                 if (page != null)
  33.                 {
  34.                     if (!page.ACL.QueryDistinctAccess(AccessLevel.Read))
  35.                         show = false;
  36.                 }
  37.             }
  38.         }
  39.         catch { }
  40.         if (show)
  41.         {
  42.             HtmlGenericControl htmlGenericControl2 = new HtmlGenericControl("li");
  43.             Literal literal = new Literal();
  44.             literal.Text = current.ToMappedLink();
  45.             htmlGenericControl2.Controls.Add(literal);
  46.             htmlGenericControl.Controls.Add(htmlGenericControl2);
  47.         }
  48.     }
  49. }

I also added a check if the link points to a episerver page that only displays links that the user have read access to.

Then

image

will return

image

and the link vg is removed

The only think I than lack is the ability to change the visitor groups in view mode. like this

image

I need to make my property implements the IPersonalizedRoles

Code Snippet
  1. [PageDefinitionTypePlugIn]
  2. [Serializable]
  3. public class PropertyLinkCollectionWithContentGroups : PropertyLinkCollection, IPersonalizedRoles
  4. {
  5.     public PropertyLinkCollectionWithContentGroups():base()
  6.     {
  7.     }
  8.     public override IPropertyControl CreatePropertyControl()
  9.     {
  10.         return new PropertyLinkCollectionWithContentGroupsControl();
  11.     }
  12.     protected string GetAttribute(LinkItem linkItem, string key)
  13.     {
  14.         if (linkItem.Attributes.ContainsKey(key))
  15.             return linkItem.Attributes[key];
  16.         return "";
  17.     }
  18.     public override IEnumerable<string> GetRoles()
  19.     {
  20.         List<string> list = new List<string>();
  21.         foreach (var link in Links)
  22.         {
  23.             var groups = GetAttribute(link, "data-groups");
  24.             if (!string.IsNullOrEmpty(groups))
  25.             {
  26.                 foreach (string id in groups.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
  27.                 {
  28.                     if (!list.Contains(id))
  29.                         list.Add(id);
  30.                 }
  31.             }
  32.         }
  33.         return list;
  34.     }
  35. }

and only return the id’s in a list of strings. then the visitor group filer will so these groups

image

The code is on property file, and one user control. If you copy those down and place them in a folder named Itera and add EPiServer.ApplicationModules and EPiServer.UI to your reference and compile then you are good to go.

Code is available here

Jun 30, 2011

Comments

Please login to comment.
Latest blogs
Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |

Unable to view Optimizely Forms submissions when some values are too long

I discovered a form where the form submissions could not be viewed in the Optimizely UI, only downloaded. Learn how to fix the issue.

Tomas Hensrud Gulla | Mar 4, 2025 |

CMS 12 DXP Migrations - Time Zones

When it comes to migrating a project from CMS 11 and .NET Framework on the DXP to CMS 12 and .NET Core one thing you need to be aware of is the...

Scott Reed | Mar 4, 2025