torsdag 29. august 2013

Hide SharePoint 2010 controls from anonymous users

I was recently working on a SharePoint 2010 Internet-facing website for a client and needed to hide the Ribbon from anonymous users. I tried various approaches to achieve this, but finally I came over a great blog post (http://blogs.msdn.com/b/zwsong/archive/2010/04/29/how-to-hide-ribbon-from-users-without-edit-page-privilege.aspx) which introduced an ingenious approach on this.

The idea is very simple really. Make usage of the SPSecurityTrimmedControl  control, but instead of wrapping the whole Ribbon DIV inside it, hide the Ribbon DIV and wrap SPSecurityTrimmedControl around a small JavaScript that shows the Ribbon.

More technical how-to (slightly modified from the original post to fit my needs):

  1. Open SharePoint Designer and open your site and your Master Page.
  2. Locate:
    <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;">
    1. >
  4. Find the end tag of "s4-ribbonrow" and add following code after it:
    <Sharepoint:SPSecurityTrimmedControl ID="spTrimRibbon"runat="server" PermissionMode="All" PermissionContext="CurrentSite" AuthenticationRestrictions="AuthenticatedUsersOnly">
        <script type="text/javascript">        document.getElementById("s4-ribbonrow").style.display = "block";
        </script>
    </
    Sharepoint:SPSecurityTrimmedControl>
  5. Save your work.
  6. That's it!
Now, this approach can be used to hide/show elements based on various rules, not just the Ribbon as illustrated in this post.

Hope you found this as useful as I did!

Thanks to Dr.Z for the original post.