Custom SPSecurityTrimmedControl

The SPSecurityTrimmedControl is a very handy control to use especially when you want to hide a control(s) or text from the user based on their permissions on the site.

I have come across situations where I needed to extend the SPSecurityTrimmed control to limit the content displayed to the current logged in user. Permissions on the site was dynamically changing so I can’t rely on using the out of the box SPSecurityTrimmed control and setting ‘static’ permissions for it to hide/show content. Also, the item is accessible by multiple users but there were some controls and text that only the author of the item should see and use.

So…… I developed my custom SPSecurityTrimmedControl.

public partial class ShowHideUIControls:Microsoft.SharePoint.WebControls.SPSecurityTrimmedControl
 {
        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            if(IsUserTheAuthor())
            {
                base.Render(output);
            }
        }

        protected bool IsUserTheAuthor()
        {
            return SPContext.Current.Web.CurrentUser.Sid.Equals(SPContext.Current.ListItem.File.Author.Sid);
        }
 }

On the aspx page where I wanted to place this control, I added the following to the top of the page.

<%@ Register TagPrefix=”CustomTrimControl” Namespace=”CustomProjectDevelopment.UserControls” Assembly=”CustomProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=o6b1a8da1u6d1903″ %>

And I added my custom SPSecurityTrimmedControl around the content and controls I wanted to hide conditionally:

<CustomTrimControl:ShowHideUIControls runat=”server” ID=”HideUIControls1″>
          <tr>
         <td style=”font-size: 133%;  padding-top: 32px; border-bottom: 1px solid #83b0ec;”>
       Site Audit Access History      
   </td>
        </tr>
 <tr><td>
 <br /> <!– Other controls or text you want to be hidden from other users are placed here e.g. like the control below –>
    <CustomDevControl:SiteAuditAccessHistory runat=”server”  id=”customDevControl1″></CustomDevControl:SiteAuditAccessHistory>
   
 </td></tr>
 </CustomTrimControl:ShowHideUIControls>

There you have it. Everything between the <CustomTrimControl:ShowHideUIControls>… <\CustomTrimControl:ShowHideUIControls> will only be visible to the author of the item.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: