SPSecurityTrimmedControl in SharePoint 2010

I loved the control SPSecurityTrimmedControl in SharePoint 2010.It conditionally renders the contents of the control to the current user only if the current user has permissions defined in the PermissionString.
Example :
<SharePoint:SPSecurityTrimmedControl ID=”SPSecurityTrimmedControl1″ PermissionsString=”BrowseDirectories” runat=”server”>
<PublishingSiteAction:SiteActionMenu runat=”server” />
<wssuc:Welcome id=”explitLogout” runat=”server” />
<PublishingWebControls:AuthoringContainer ID=”authoringcontrols” runat=”server”> <PublishingConsole:Console runat=”server” />
</PublishingWebControls:AuthoringContainer>
</SharePoint:SPSecurityTrimmedControl>
Note here Permission string is “Browsepermission”. List of permission strings can be found in the msdn articlehere
SPSecurityTrimmedControl is very useful to display content to a particular set of users hiding others.
One of the practical usage of this is hiding and showing ribbon to authenticate users. Here is how you can do it.
<SharePoint:SPSecurityTrimmedControl ID=”IsAuthenticatedControl1″ runat=”server” AuthenticationRestrictions=”AuthenticatedUsersOnly”>
<asp:Button runat=”server” Text=”Button” id=”Button1″></asp:Button>
</SharePoint:SPSecurityTrimmedControl>
Now this button will be only shown to the authenticated user.
What if you want to hide the Ribbon for Visitors and the other users who don’t have a permission  to the page.
Here is how you can do it
1) Open your SharePoint master page
2) Locate this line:
<div id=”s4-ribbonrow” class=”s4-pr s4-ribbonrowhidetitle”>
3) Change it to:
<div id=”s4-ribbonrow” class=”s4-pr s4-ribbonrowhidetitle” style=”display:none”>
4) Now find the end of the “s4-ribbonrow” tag and add following block right after it:
<Sharepoint:SPSecurityTrimmedControl ID=”SPSecurityTrimmedControl1″ runat=”server” PermissionsString=”AddAndCustomizePages”>
<script type=”text/javascript”>
document.getElementById(“s4-ribbonrow”).style.display = “block”;
</script>
</Sharepoint:SPSecurityTrimmedControl>
5) Save the new master page and publish it.

Here If you can notice..I am rendering the javascript and make the ribbon visible, if the user has a permission to add and customize the page.

Comments