1

I managed to hide the Gear icon from non-admin users. I can apply CSS, but it will hide it from all users.

Is there a similar way to hide the Search Box from non-admin users only?

0

You can check the current page context for the users base permissions. This code should give the general idea of how it works. You'll want to set this into the sites master page below the "body" tags.

There may be a better solution than this, but this is the best I know.

<script language="javascript">
$(function() {
//Get the SearchBox element. May need to be changed depending on environment.
var SearchBox = document.getElementById("SearchBox")

// Get the context for the current page.
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();

// Set the minimum base permission to see the search box.
var ob = new SP.BasePermissions();
ob.set(SP.PermissionKind.manageWeb)

// Check if current user has the required permissions
var per = web.doesUserHavePermissions(ob)

ctx.executeQueryAsync(
    function(){
        // On successful callback
        if(per.get_value()){
            // If user has permissions, leave the style to inherit or some other value.
            SearchBox.style.display = "inherit"
        }
        else {
            // If the user doesn't, hide the search box.
            SearchBox.style.display = "none"
        }
    },
    function(a,b){ 
        // continue on fail 
    });
});
</script>

If you want to check for a different base permission level, please reference the following site. It gives the list of other enumerations available.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.permissionkind.aspx

0

If you have rights to edit master page then you can use security trim control and can assign Permission level to it. use below code:

<Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="ManageWeb">
    <SharePoint:AjaxDelta id="DeltaPlaceHolderSearchArea"  BlockElement="true" CssClass="ms-mpSearchBox ms-floatRight" runat="server">
        <asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server">
           <div id="searchInputBox">
              <SharePoint:DelegateControl runat="server" ControlId="SmallSearchInputBox" />
           </div>
        </asp:ContentPlaceHolder>
    </SharePoint:AjaxDelta>
</SharePoint:SPSecurityTrimmedControl>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.