11

Is there a tutorial or specific bit of code I can use to hide the ribbon for all users except admins or a user group?

This needs to be for sharepoint 2013 (rather than 2010)

Can anyone help please?

uatonly
  • 363
  • 2
  • 11

2 Answers2

10

How about Security Trimming that Ribbon in the masterpage?

<!--CS: Start Security Trim Snippet-->
    <!--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="AuthenticatedUsersOnly" Permissions="AddAndCustomizePages" PermissionContext="RootSite">-->
        <!--PS: Start of READ-ONLY PREVIEW (do not modify)--><span><!--PE: End of READ-ONLY PREVIEW-->
            <div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">
            ...
            </div>
        <!--PS: Start of READ-ONLY PREVIEW (do not modify)--></span><!--PE: End of READ-ONLY PREVIEW-->
    <!--ME:</SharePoint:SPSecurityTrimmedControl>-->
    <!--CE: End Security Trim Snippet-->

MSDN Reference:

https://msdn.microsoft.com/en-us/library/office/jj822366.aspx

Lucas Rodrigues
  • 538
  • 1
  • 4
  • 15
9

CSS + JS Solution

Add following CSS in your master page

#RibbonContainer{
  display: none;
}

Now GET current users details

/_api/web/currentuser?$expand=Groups

Make a GET request to the above end-point, it will return current user's details with his/her Groups

Now check if current user IsSiteAdmin, then display Ribbon again.

if (response.d.IsSiteAdmin){
   document.querySelector("#RibbonContainer").style.display = 'block';
}

If you need to check if current user exists in particular Group, then

var groupName = "Your Group Name";

var isUserExistsInGroup = response.d.Groups.results.some(function(g) { return g.Title == groupName; });

if (isUserExistsInGroup) { document.querySelector("#RibbonContainer").style.display = 'block'; }

Full JS Code put it in your master page

(function() {
    var xmlhttp = new XMLHttpRequest();
xmlhttp.open(&quot;GET&quot;, _spPageContextInfo.webAbsoluteUrl + '/_api/web/currentuser?$expand=Groups');

xmlhttp.setRequestHeader(&quot;Accept&quot;, &quot;application/json;odata=verbose&quot;);

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE) {
        if (xmlhttp.status == 200) {
            var response = xmlhttp.responseText;

            if (response.d.IsSiteAdmin) {
                document.querySelector(&quot;#RibbonContainer&quot;).style.display = 'block';
            }

            var groupName = &quot;Your Group Name&quot;; //Give here your Group Name

            var isUserExistsInGroup = response.d.Groups.results.some(function(g) {
                return g.Title == groupName;
            });

            if (isUserExistsInGroup) {
                document.querySelector(&quot;#RibbonContainer&quot;).style.display = 'block';
            }

        } else {
            alert('Error: ' + xmlhttp.statusText)
        }
    }
}

xmlhttp.send();

})();

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
  • Thank you @Atish, i'm trying this. I have added the CSS snipet in the master css file second JS snippet to JS file - I then amended the group name. Its not currently working - have i missed something? do these all need to be added to the masterfile? Thank you for your help – uatonly Jan 12 '16 at 11:19
  • Script also should be in Master page. So me your code. Do you have jquery in you master page – Atish Kumar Dipongkor Jan 12 '16 at 11:23