Display Web Part Content Based on User Group by Extending Security Trimmed Control in SharePoint 2013

Ashok Raja
 
Solutions Architect
December 21, 2012
 
Rate this article
 
Views
819

In this article we can see how to extend the SharePoint 2013 Security Trimmed Control to show or hide part of the web part content. The default Security Trimmed control accepts permission types as input parameter to render content. But this extended Security Trimmed control accepts User Groups as its input parameter to render content.

Now let us see how we can extend the Security Trimmed Control. To begin with, create a new Share Point 2013 project and add a class and rename it to SecurityTrimmedControlEx.cs. Inherit that class from SPSecurityTrimmedControl , which in turn requires a reference to Microsoft.SharePoint.WebControls name space. Then override the render method to conditionally call base.Render method depending on the User Groups passed as input parameter to the control.

To have a better usability, I have added an additional option of setting filter type. Through this filter type you can decide whether to allow or block User Groups for a specific section in the Visual Web Part. This control accepts multiple User Groups as input parameter and this can be passed as comma separated values.

The below is the source code of SecurityTrimmedControlEx.cs class

 using System;
 using Microsoft.SharePoint.WebControls;
 using Microsoft.SharePoint;
 
 namespace SFS.Intranet.Controls.ControlEx
 {
     /// <summary>
     /// SharePoint Frontier Extended Secturity Trimmed Control by Ashok Raja .T 
     /// To get updated on latest happening around SharePoint world , do visit my blog @ https://www.sharepointpals.com
     /// </summary>
     public class SecurityTrimmedControlEx : SPSecurityTrimmedControl
     {
         public enum FilterTypes
         {
             Allow,
             Block
         }
 
         /// <summary>
         /// Gets or sets the type of the filter.
         /// </summary>
         /// <value>
         /// The type of the filter.
         /// </value>
         public FilterTypes FilterType { get; set; }
 
         /// <summary>
         /// Gets or sets the user groups.
         /// </summary>
         /// <value>
         /// The user groups.
         /// </value>
         public string UserGroups { get; set; }
 
         /// <summary>
         /// </summary>
         /// <param name="output">An EventArgs object that contains the event data.</param>
         protected override void Render(System.Web.UI.HtmlTextWriter output)
         {
             if (string.IsNullOrEmpty(UserGroups))
             {
                 base.Render(output);
                 return;
             }
             if (FilterType == FilterTypes.Allow)
             {
                 if (isAllowed())
                     base.Render(output);
             }
             else
             {
                 if (!isAllowed())
                     base.Render(output);
             }
         }
 
         /// <summary>
         /// Determines whether User Group is allowed.
         /// </summary>
         /// <returns>
         ///   <c>true</c> if User Group is allowed; otherwise, <c>false</c>.
         /// </returns>
         private bool isAllowed()
         {
             try
             {
                 SPWeb site = SPContext.Current.Web;
                 string[] Groups = UserGroups.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                 if (Groups.Length == 0)
                     return false;
                 foreach (string item in Groups)
                 {
                     if (item.Trim().Length == 0)
                         continue;
                     int groupId = site.Groups[item].ID;
                     if (site.IsCurrentUserMemberOfGroup(groupId))
                         return true;
                 }
                 return false;
             }
             catch (Exception Ex)
             {
                 return false;
             }
         }
     }
 }

To validate this Extended Security trimmed control, I have created 3 Share Point User Groups named as User Group 1, User Group 2 and User Group 3 to a SharePoint Site and  have created 3 user accounts named as u1, u2 and u3 and assigned u1 to User Group 1 and u2 to User Group 2 and so on. I have added a new Visual Web Part to this project and have created multiple content areas to show or hide web part content for different user groups. The below is extract of design part of Visual Web Part.

 <%@ Register TagPrefix="SFS" Namespace="SFS.Intranet.Controls.ControlEx" Assembly="$SharePoint.Project.AssemblyFullName$" %>
 Logged In User : <b>
     <asp:Literal ID="l1" runat="server"></asp:Literal></b>
 <br />
 <SFS:SecurityTrimmedControlEx ID="s1" runat="server" FilterType="Allow" UserGroups="User Group 1">
     <div>
         Content for User Group 1 with allow filter
     </div>
 </SFS:SecurityTrimmedControlEx>
 <SFS:SecurityTrimmedControlEx ID="s2" runat="server" FilterType="Allow" UserGroups="User Group 2">
     <div>
         Content for User Group 2 with allow filter
     </div>
 </SFS:SecurityTrimmedControlEx>
 <SFS:SecurityTrimmedControlEx ID="s3" runat="server" FilterType="Allow" UserGroups="User Group 3">
     <div>
         Content for User Group 3 with allow filter
     </div>
 </SFS:SecurityTrimmedControlEx>
 <SFS:SecurityTrimmedControlEx ID="s4" runat="server" FilterType="Block" UserGroups="User Group 3,User Group 1">
     <div>
         Content Blocked for User Group 3 and User Group 1 with filter type set to block
     </div>
 </SFS:SecurityTrimmedControlEx>

Please find below the screen shot of the web part taken after logging in into the application with different user account.

u1

u2

Note : This post contains downloadable sample code for both SharePoint 2013 and SharePoint 2010.

Author Info

Ashok Raja
 
Solutions Architect
 
Rate this article
 
I am Ashok Raja, Share Point Consultant and Architect based out of Chennai, India. ...read more
 

Leave a comment