Selectively hiding contents in SharePoint page


 

Have you ever needed to hide certain contents on your SharePoint page, be it a control or custom javascript? Luckily, SharePoint provides a control to allow you to do just that. The control is SPSecurityTrimmedControl. If you use SharePoint Designer to edit your page and insert this control, anything within this control will only be rendered on your page if it passes the criteria. The criteria it uses is SharePoint permission. You can easily google/bing this control for more information.

However, this control does not meet my requirements. I needed to selectively provide contents based on SharePoint groups, not permissions, since multiple groups can have the same permissions. If you ever have to meet this kind of requirement, do not panic. The SPSecurityTrimmedControl can be easily extended. Below is the code I created for my custom control that extends SPSecurityTrimmedControl.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace Yaohan.SharePoint.CustomControls
{
    public class SPGroupTrimmedControl : SPSecurityTrimmedControl
    {
        private string groupName;
        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; }
        }
        protected override void Render(HtmlTextWriter output)
        {
            if (IsUserInGroup())
                base.Render(output);
        }
        private bool IsUserInGroup()
        {
            bool retVal = false;
            foreach (SPGroup group in SPContext.Current.Web.Groups)
            {
                if (group.Name.Equals(GroupName))
                {
                    retVal = group.ContainsCurrentUser;
                }
            }
            return retVal;
        }
    }
}

What you need to do is whip up Visual Studio and create a web control project. Make sure you “strong name” the project and sign it.

To deploy, you can drop the DLL in GAC, however I will strongly advise you to package this up using SharePoint solution, which should be simple enough to do (create manifest.xml, reference the DLL on assemblies tag and the safe control tag).

After you deploy your DLL, you need to register your custom control, either on your page (if it’s page-specific), page layout or masterpage. Below is the snippet of control registration

<%@ Register Tagprefix="Yaohan" Namespace="Yaohan.SharePoint.CustomControls" Assembly="Yaohan.SharePoint.CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e15ae9471d9f940" %>

And to use the control in your page, you can do the following:

<Yaohan:SPGroupTrimmedControl ID="GroupTrimmedControlForReaders" GroupName="Yaohan Special Group" runat="server">

Kudos to Yaohan special group only.

</Yaohan:SPGroupTrimmedControl>

Now you can extend this control to selectively display contents to not just SharePoint group, but AD group (ala target audience). Multiple groups, or collection of users.

You may ask why you should do this instead of using content editor webpart with target audience. In my scenario, I cannot use content editor webpart, because I need a quick and easy way to hide fields in item display form based on the group the current user belong to. For example, if you have details of asset inventory, I only want to show a general information about the assets for read-only general readers (such as description, photo, etc), but for specific group, I want to show more details.

The way I achieve this is by having a javascript between my custom controls to hide the fields I don’t want to show. Beware that the javascript is only to hide the information, not securing it, thus if users know what they’re doing and view the page source, they can still find the information. If you want a field-based security, there is a third party product that caters for that.

Credit goes to http://blah.winsmarts.com/2008-5-Enhancing_the_SPSecurityTrimmedControl_-_Trimming_UI_on_any_critereon.aspx for providing me inspiration.