Code to Hide the Ribbon and Site Actions Menu for Anonymous Users

In Beta 2, SharePoint 2010 sites that use the publishing infrastructure have an option on the Navigation Settings page to hide the ribbon by default.

Other sites lack this functionality as does SharePoint Foundation. However, the code to do the hiding is simple:

public void HideRibbonForAnon()
{
  SPRibbon current = SPRibbon.GetCurrent(this.Page);

  if (current != null && !this.Page.Request.IsAuthenticated)
  {
    current.CommandUIVisible = false;

    SiteActions actions = SiteActions.GetCurrent(this.Page);

    if (actions != null)
    {
       actions.Visible = false;
    }
    HideStatusBar();
  }
}
internal void HideStatusBar()
{

   string script = 
      "document.onreadystatechange=fnRemoveAllStatus; function fnRemoveAllStatus(){removeAllStatus(true)};";
   this.Page.ClientScript.RegisterClientScriptBlock(typeof(HideTheRibbon), "statusBarRemover", script, true);
}

 

Author: Doug Ware