Wednesday, February 1, 2012

Hiding SiteActions in the Ribbon



There are multiple ways of hiding the SiteActions in the Ribbon:

1. Hiding in the master page
    This is probably the simplest version. 
    Open the master page in Sharepoint designer or Visual Studio and search for <Sharepoint:SiteActions> control. Enclose this content inside a SPSecurityTrimmedControl. See code below:
<SharePoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl1" runat="server" PermissionsString="ManageSubwebs">

<<SharePoint:SiteActions control>>


</SharePoint:SPSecurityTrimmedControl>

The SPSecurityTrimmedControl checks for permission of every user and only if the user has got required permissions, it will show the content within this control.  In the above code, only if the user has got ManageSubwebs permission, he/she will be able to see the SiteActions.

If you want to hide/show SPRibbon based on permissions, enclose the SPRibbon control within the SPSecurityTrimmedControl. 

2. Using JQuery
     If you do not have access to edit the Master Page, this is the next simplest solution. However the major disadvantage of this method is you need to add the JQuery script to either Content Editor WebPart or to the Page Layout. This will not work in case of the default Sharepoint pages like Document library pages or List pages. 

Refer this post for the script. 

3. Delegate control that will update MasterPage on the fly
The best solution to achieve this when you do not have access to MasterPage is by creating a delegate control and activating it.


  • Create an Empty Sharepoint Project
  • Create a class that inherits Control and add the below code:
    protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            HideSiteActions();

        }

        private void HideSiteActions()

        {

            MasterPage masterPage = Page.Master;

            if (masterPage == null)

                return;

            SiteActions siteActions = masterPage.FindControl("SiteActionsMenuMain") as SiteActions;

            /* … Add the code to check for users permissions  … */

                                siteActions.Visible = false;

        }

  • Create an Empty Element file and add a Control tag to the element.xml file
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Control Id="AdditionalPageHead" Sequence="50"

          ControlClass="HideSiteActions.HideSiteActionsClass"

          ControlAssembly="$SharePoint.Project.AssemblyFullName$"></Control>

</Elements>

  • Set the scope of the feature to Site
  • Set the Include Assembly in Package property of the project to false
  • Add a Safe control entry in the package
    • Open the .package and go to Advanced tab
    • Add Assembly




  • Deploy the project









No comments:

Post a Comment