As you probably know OTB Search box web part allows user to specify search keywords and perform search across configured search scopes. There are 2 types of scopes:
- contextual – means that they depend on the current context, i.e. on where user is currently located on the site. Examples of contextual scopes are “This site” (search will be done in the current site and its sub sites) and “This list” (search will be done only in the current list or doclib);
- custom – scopes configured in Central administration. Examples are “All sites” (search will be done across all Sharepoint sites in specified content source) and “People” (search will be done only across people).
Both contextual and custom scopes are shown in the scopes dropdown list near the text box in Search box web part. It is possible to control what scopes are shown there. E.g. in “Dropdown mode” property we may specify one of the following values:
- Do not show scopes drop down
- Show scopes drop down
- Show, and default to ‘s’ URL parameter
- Show and default to contextual scope
- Show, do not include contextual scopes
- Show, do not include contextual scopes, and default to ‘s’ URL parameter
They are quite self-explanative. The problem however is that using this property we may only hide all scopes or hide only contextual scopes. If we want to leave only contextual scopes and hide all custom scopes we need to set another web part property “Scope display group” to empty value. Scope display groups themselves are configured in Site settings > Search scopes and include custom scopes configured in Central administration.
By default search box web part redirects user to OTB OSSSearchResults.aspx application layout page which uses default application.master master page and as result has OTB look and feel. If we want to use custom look and feel we need either to use search center site collection or create new publishing page on the site and add Search core results web part on it. After that we need to specify URL of this page in the “Target search results page URL” property of Search box web part. The problem is that this property is used only for custom scopes, but not for contextual scopes. For the last ones it is ignored and user is anyway redirected to the OTB OSSSearchResults.aspx page.
In order to fix this issue we may use the following solution: create custom web part which inherits OTB SearchBoxEx control and sets its private variable “m_strOssSearchResultsUrl” via reflection (exactly this variable contains URL of the page used for search in contextual scopes):
Here we used ReflectionHelper class:
After that it will use custom search page URL also for contextual scopes.