Friday, December 3, 2010

Hiding Ribbon and Navigation on External Facing SharePoint 2010 Site


Suppose you have a SharePoint 2010 Publishing or External Facing Web Site, which means by definition you will have enabled Anonymous Access to the Site to allow Web visitors to access it. Unless the SharePoint Navigation and Ribbon fit in with your design, sooner or later you will start looking for a way to only show them to logged in or admin users. There are two way to accomplish this (thanks to Doug Ware for his hint):

The Hard Way:

One way to do this is to create custom login in the Master Page of your SharePoint Site by enabling scripting on it. To do this, you have to first enable scripting on the Master Page.

  • Open the Web.config file of your SharePoint Site in Notepad.
  • In Web.config file, find the following section:

  •  <SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">  
         <PageParserPaths>  
    

  • Add the following XML Node to this section:

  •  <PageParserPath VirtualPath="/*" IncludesSubFolders="true" CompilationMode="Always" AllowServerSideScript="true" />  
    

  • Save and Close the Web.Config file. 
  • Go to Start > Run > Type "cmd" to open Command Prompt. 
  • Type "iisreset" and hit ENTER to reset IIS. 
  • In the SharePoint Master Page, add the following code block around the SharePoint Ribbon or Navigation control you wish to hide:

  •  <div runat="server" ID="ctlToHide">  
         <!-- Your Ribbon Code to Hide Goes Here -->  
     </div>  
    

  • Add The Script to Hide this section:

  •  <script runat="server">  
     protected void Page_Load(object sender, EventArgs e)  
     {  
         if (Context.User.Identity.Name == "domainname\username")   
         {   
             ctlToHide.Visible = true;   
         }   
         else  
         {  
             ctlToHide.Visible = false;  
         }  
     }  
     </script>  
    

The Easy Way:

Add the following Tags around each section you wish to hide in your SharePoint Master Page:

 <Sharepoint:SPSecurityTrimmedControl runat=”server” Permissions=”ManageLists”>  
     <!-- Your Ribbon Code Goes Here -->  
 </SharePoint:SPSecurityTrimmedControl>  

Obviously the second route besides being much easier is also a lot more secure then the first route! So don't go scripting in the Master Page if you don't have to!

No comments:

Post a Comment