Hiding Contextual Search Scopes in SharePoint using JavaScript

In SharePoint 2007, custom Search scopes can be enabled in a Site Collection through Site Settings > Search Settings page. You can use Site Settings > Scopes page to view and manage search scopes. After configuring, you will notice that the search scopes dropdown list has few more search scopes which are commonly referred to as "Contextual" search scopes - "This Site: <site name>" or "This List: <list name>". These cannot enabled/disabled from any settings page.

Before: Search scopes dropdown in the portal homepage


Before: Search scopes dropdown in a list page:


Search results for searches in custom scopes, open in the Search Center results page specified by you in settings page. But, when a contextual search scope is selected, search results open in /_layouts/osssearchresults.aspx page. This is a tagged as a SharePoint behaviour by some users but most consider it a problem. User experience is different in both pages and osssearchresults.aspx page cannot be easily modified.

Mark Arend has covered the concept and problem in his blog post - Search Results Page may be different per Search Scope

Solution using JavaScript

My approach to solve the problem was to just hide the contextual search scopes from the dropdown and not to touch either the osssearchresults.aspx page or any SharePoint Feature files. I did it using a quick and dirty JavaScript code in the Master page.

I inserted following code between the <HEAD> and </HEAD> tags. This code gets the search scopes dropdown list and removes all options which contain a specified character. In this scenario, I intend to remove all options which contain ":" character.

<script type="text/javascript">
        function removeOption(checkChar) {
            var x = document.getElementById("ctl00_PlaceHolderSearchArea_ctl01_SBScopesDDL");
            for (i = 0 ; i < x.length; i++) { if (x.options[i].text.indexOf(checkChar) != -1) { x.remove(i); i--; } } }

Then call the above function in onload event of BODY tag

<BODY scroll="yes" onload="BLOCKED SCRIPTif (typeof(_spBodyOnLoadWrapper) != 'undefined') { _spBodyOnLoadWrapper(); removeOption(':'); }">

Save the Master page and now check the pages.

After: Search scopes dropdown in the portal homepage

After: Search scopes dropdown in the list page


2 other approaches available to solve the problem are:

Redirect Contextual Search Results to Search Centre Results Page

Removing contextual: This sites cope from simple search box

Comments (0)

Skip to main content