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









Rounded corner borders in HTML

There is no straight-forward method for rounded-corner border in HTML. We need to implement this in CSS. The HTML and CSS scripts for this is:



<style type="text/css">
                #outer h1, #outer h2, #outer p {margin:0 10px; letter-spacing:1px; padding:0;}
                #outer h1 {font-size:2.5em; color:#fff;}
                #outer h2 {font-size:2em;color:#06a; border:0;}
                #outer p {padding-bottom:0.5em;}
                #outer h2 {padding-top:0.5em;}
                #outer {background: transparent; margin:25px 0.5em 25px 1em;}
                .xtop, .xbottom {display:block; background:transparent; font-size:1px;}
                .xb1, .xb2, .xb3, .xb4 {display:block; overflow:hidden;}
                .xb1, .xb2, .xb3 {height:1px;}
                .xb2, .xb3, .xb4 {background:#F8FBF4; border-left:1px solid #E2E3E2; border-right:1px solid #E2E3E2;}

                .xb1 {margin:0 5px; background:#E2E3E2;}
                .xb2 {margin:0 3px; border-width:0 2px;}
                .xb3 {margin:0 2px;}
                .xb4 {height:2px; margin:0 1px;}

                .xboxcontent {display:block; background:#F8FBF4; border:0 solid #E2E3E2; border-width:0 1px;}
</style>
     
                <div id="outer">
                                <b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b>
                                <div class="xboxcontent">
            <p>
                This text will appear inside the rounded-corner border :)
            </p>
        </div>
                                <b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
                </div>

Hide Site Actions based on User permissions

The below JQuery will hide the Site Actions menu for all non-admin users:


<script src="/sites/MySPSite/Style%20Library/XSL%20Style%20Sheets/jquery-1.4.2.js" type="text/javascript"></script>
<script src="/sites/MySPSite/Style%20Library/XSL%20Style%20Sheets/jquery.SPServices-0.7.0.js" type="text/javascript"></script>


<script type="text/javascript">
$(document).ready(function() {
   $().SPServices({
//Call the function GetRolesAndPermissionsForCurrentUser from SPServices.js
     operation: "GetRolesAndPermissionsForCurrentUser",
     userLoginName: $().SPServices.SPGetCurrentUser(),
     async: false,
     completefunc: function(xData, Status) {
        var userPerm = $(xData.responseXML).find("Permissions").attr("Value");
        var admin = 9223372036854775807; //Hex code for Admin group
        if(admin != userPerm)
        {
            $("span.ms-siteactionsmenu").css("display", "none");
        }
      }
   });
});


</script>