0

I want to show/hide the search box based on the current logged in user.

I have one permission group for e.g., xyz. this group has contribute permission to only document library(not even to the web). I have a requirement such that if user from group xyz loggs in to the system then the search box should not appear to that user. Users from other than this group should see the search box.

I came to know about the <SharePoint:SPSecurityTrimmedControl> by this link. And in the master page, I placed the divs inside <asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server"> to this control. But the search box got hide forever for all users.

The master page contains following code:

 <asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server"> 
    <SharePoint:SPSecurityTrimmedControl ID="TrimmedSearch" runat="server" PermissionsString="ViewPages" PermissionContext="RootSite">
    <div class="SearchBoxArea">
    <div class="addtolink menu-item-text ">
    <script type='text/javascript'>var pageUrl = document.URL; var Id = 0; var SuitePanel = 'SuitePanel'; </script> 
           </div>
    <div id="searchInputBox">       
      <SharePoint:DelegateControl ID="DelegateControl5" runat="server" ControlId="SmallSearchInputBox" />
    </div>
    </div>
    </SharePoint:SPSecurityTrimmedControl>
  </asp:ContentPlaceHolder>

What am I missing?

0

Firstly <SharePoint:SPSecurityTrimmedControl> works based on the type of permission the user has, not on the group which user is. For eg: If there is another group with the same level of permission as the group for which you do not want to show the Search box , then users of that other groups will also not see the SearchBox(since both the groups have same level of permission).

The Best way to do this is to use JSOM , and add this code to a javascript file and reference it in masterpage. Below is a snippet to start with:

$(document).ready(function () {
// Make sure SP Services is loaded
     if ($().SPServices == null) {
        jQuery.getScript("/style library/js/jquery.SPServices.min.js", isGroupMember);
    } else {
        isGroupMember();
    }
});

function isGroupMember() {
    $().SPServices({
        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: true,
        completefunc: function (xData, Status) {
            if($(xData.responseXML).find("Group[Name='yourgroupname']").length == 1){
             // hide searchbox using javascript here
            }
        }
    });
};

You should download SPServices library and update the path in the code.

  • Hello sir, Thanks for responding, but my requirement is simple enough that if user has read permission in the root site, then search box should appear other wise not. Then should I go for the script? – users1100 Apr 29 '15 at 10:54
  • Okay then the <SharePoint:SPSecurityTrimmedControl> is perfect for you. – Unnie Apr 29 '15 at 11:28

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.