Hiding SharePoint Page Elements Based on the Currently Logged in User

Written by Denis Stadler on . Posted in Custom Development

You may get this business requirement in the one following scenarios:

– You want to exclude part of a page (for example static links in the Master Page) from being indexed by SharePoint Search
– Just hide page elements for a specific user

For the Search scenario in SharePoint 2010 there is the possibility of using a div with CSS class name equal to noindex for not having the content crawled, but this doesn’t work in MOSS 2007.

<div class=”noindex”>content no to be indexed</div>

SharePoint has a build in feature which filters content based on the security roles of the user: SPSecurityTrimmedControl.

But the is no class for content trimming based on the loged-in user account, and we will create it :). The advantage is that this should work both in SharePoint 2010 and MOSS 2007.

Phase 1: Development

The C# class doesn’t contain more than:

public class SPUserSecurityTrimmedControl : SPSecurityTrimmedControl

public string ExcludeUser
get; set;

protected override void Render(HtmlTextWriter output)
SPWeb site = SPContext.Current.Web;

if (site.CurrentUser.LoginName.ToLower() != ExcludeUser.Trim().ToLower())

SharePoint 2010 Full Code

MOSS 2007 Full Code

Phase 2: Deployment

Basically the deployment process should have the following steps:

1) Deploy the DLL into GAC.

1) Go to the Site Collection’s WebConfig (usually in “C:\Inetpub\wwwroot\wss\VirtualDirectories\<host header><port number>“) and in the <SafeControls> XML tag add the following line:

<SafeControl Assembly=”Stadler.UserSecurityFilter, Version=, Culture=neutral, PublicKeyToken=718119e36b4b9c5f” Namespace=”Stadler.UserSecurityFilter” TypeName=”*” Safe=”True” />

2) Add the following line in the Master Page (or another page… ).

<%@ Register Tagprefix=”UserTrimSecurity” Namespace=”Stadler.UserSecurityFilter” Assembly=”Stadler.UserSecurityFilter, Version=, Culture=neutral, PublicKeyToken=718119e36b4b9c5f” %>

3) Within the Master Page add the following tags in order to be able to hide / show content based on the user.

<UserTrimSecurity:SPUserSecurityTrimmedControl ExcludeUser=”DENIS\user” runat=”server”>The text is not visible if you are loged in with <b>DENIS\user</b>. </UserTrimSecurity:SPUserSecurityTrimmedControl>

4) Check-In the Master Page and Approve It.

Tags: ,

Trackback from your site.

Denis Stadler

I'm a technology enthusiast, with more than 10 years of experience in SharePoint and Dynamics CRM projects. To find more details about, please visit the about me page.

Leave a comment