Hide SharePoint Online/On Premise Ribbon for Anonymous Users

Essentially all public facing SharePoint sites, including those hosted on SharePoint Online or on-prem SharePoint 2013, require a method to hide the suite bar / ribbon for anonymous users. The SharePoint Online oslo master page does an OK job of this while the seattle master page does not even attempt to hide the ribbon. I have been asked quite often how I prefer to hide the ribbon for anonymous users, or show some content to authenticated users only, etc. This blogs walk you through the steps to achieve this goal

The primary method everyone jumps to is the control, <SharePoint:SPSecurityTrimmedControl /> found in the Microsoft.SharePoint.WebControls namespace, typically registered to the tag SharePoint. As an example.

Show only for Anonymous Users:

<SharePoint:SPSecurityTrimmedControl runat=“server” AuthenticationRestrictions=“AnonymousUsersOnly” EmitDiv=“true”>
</SharePoint:SPSecurityTrimmedControl>

Show only for Authenticated Users:

<SharePoint:SPSecurityTrimmedControl runat=“server” AuthenticationRestrictions=“AuthenticatedUsersOnly” EmitDiv=“true”>
</SharePoint:SPSecurityTrimmedControl>

The often quoted, yet incorrect, solution for hiding the SharePoint ribbon for anonymous users is to encapsulate the entire suite bar / ribbon with the SPSecurityTrimmedControl with AuthenticationRestrictions set to AuthenticationRestrictions. The problem with this solution is that the out-of-the-box SharePoint JavaScript that is used to determine the height of the s4-workspace requires a div with an id, s4-ribbonrow.

My preferred SharePoint Online and SharePoint 2013 solution for hiding the suite bar / ribbon for anonymous users uses the above theory while also addressing the #s4-ribbonRow requirement. I also find is much cleaner than what is found in the oslo master page.

Simply replace the suite bar / ribbon found in your master page with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
<div id=“ms-designer-ribbon”>
<SharePoint:SPSecurityTrimmedControl runat=“server” AuthenticationRestrictions=“AnonymousUsersOnly” EmitDiv=“true”>
<div id=“s4-ribbonrow” style=“visibility:hidden;display:none”></div>
</SharePoint:SPSecurityTrimmedControl>
<SharePoint:SPSecurityTrimmedControl runat=“server” AuthenticationRestrictions=“AuthenticatedUsersOnly” EmitDiv=“true”>
<div id=“suiteBar” class=“ms-dialogHidden noindex”>
<!–#suiteBarLeft and #suiteBarRight–>
</div>
<div id=“s4-ribbonrow”>
<!–ootb ribbon controls–>
</div>
</SharePoint:SPSecurityTrimmedControl>
</div>

Highlights

Notice that I wrap the entire suite bar / ribbon in my own wrapper. This ID came from Design Manager, but if you have a wrapper around the suite bar / ribbon it will be easier to target specific elements later.

I then include a SPSecurityTrimmedControl for anonymous users that only includes the #s4-ribbonrow div (hidden) while the second SPSecurityTrimmedControl control includes your standard suite bar and ribbon controls.

If you are using design manager, I suggest the following code for your HTML Master Page.

1
2
3
4
5
6
7
8
9
<div id=“ms-designer-ribbon”>
<!–SPM:<%@Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”%>–>
<!–MS:<SharePoint:SPSecurityTrimmedControl runat=”server” AuthenticationRestrictions=”AnonymousUsersOnly” EmitDiv=”true”>–>
<div id=“s4-ribbonrow” style=“visibility:hidden;display:none”></div>
<!–ME:</SharePoint:SPSecurityTrimmedControl>–>
<!–MS:<SharePoint:SPSecurityTrimmedControl runat=”server” AuthenticationRestrictions=”AuthenticatedUsersOnly” EmitDiv=”true”>–>
<!–SID:02 {Ribbon Snippet}–>
<!–ME:</SharePoint:SPSecurityTrimmedControl>–>
</div>

What about a Sign In Control?

Adding a Sign In control that is only available to anonymous users is just as easy. We can again use the SPSecurityTrimmedControl control along with the Welcomecontrol template. This block of code could be placed anywhere in your Master Page (or HTML Master Page) that your UI design requires.

1
2
3
4
<%@ Register TagPrefix=“wssuc” TagName=“Welcome” src=“~/_controltemplates/15/Welcome.ascx” %>
<SharePoint:SPSecurityTrimmedControl runat=“server” AuthenticationRestrictions=“AnonymousUsersOnly”>
<wssuc:Welcome id=“WelcomeLogin” runat=“server” EnableViewState=“false” />
</SharePoint:SPSecurityTrimmedControl>

Or for a HTML Master Page in Design Manager,

1
2
3
4
<!–SPM:<%@ Register TagPrefix=”wssuc” TagName=”Welcome” src=”~/_controltemplates/15/Welcome.ascx” %>–>
<!–MS:<SharePoint:SPSecurityTrimmedControl runat=”server” AuthenticationRestrictions=”AnonymousUsersOnly”>–>
<!–SPM:<wssuc:Welcome id=”WelcomeLogin” runat=”server” EnableViewState=”false” />–>
<!–ME:</SharePoint:SPSecurityTrimmedControl>–>

 

Thanks, Please let me know if you have any question

Tesfaye Gari

Leave a Reply

ADDRESS

901 S Highland St Room 314
Arlington, Virginia 22204
Phone: (240) 547-6851

About MTM

MTM Training and Consulting is started by a group of highly qualified and motivated Information Technology Professionals to help you boost your IT career, and your company's productivity
Read More