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?

4

2 Answers 2

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("GET", _spPageContextInfo.webAbsoluteUrl + '/_api/web/currentuser?$expand=Groups');

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

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

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

                var groupName = "Your Group Name"; //Give here your Group Name

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

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

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

    xmlhttp.send();

})();
2
  • 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, 2016 at 11:19
  • Script also should be in Master page. So me your code. Do you have jquery in you master page Jan 12, 2016 at 11:23
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

0

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .