In April 2019, Kerem Yuceturk posted an article on the Tech Community site titled ‘Configure modern search results to search all of your organization (rather than the current site‘. He stated that ‘… there is a desire for being able to change the behavior to always search over the whole organization, or across the hub a site is associated with, without needing an additional click, especially if the site in question will be used as a modern landing page for your organization’.
This post examines what happens when the search scope is changed from the default setting, and how it can be reset.
What should happen
Normally, when you open a SharePoint site and enter search terms where it says ‘Search this site’ it will return results from that specific site. This is because the ‘search scope’ should be set to either ‘0’ (Default Scope = Site).
An example of standard site search results is shown below. The end part of the search results URL is: /_layouts/15/search.aspx/siteall?q=Example*
If the user wants to then search across all sites in the tenant they have access to, they can click on the word ‘Organization’ at the top of the page. The end part of the search result URL when ‘Organization’ is: /_layouts/15/search.aspx?q=Example*
What happens if the search scope is modified
If the site (or all sites) search scope is changed from the default (‘0’ or ‘3’ for the site) to 1 (Tenant), the initial search will return results from every site in the tenant.
As we will see below, the text in the search box may still say ‘Search this site’ but the search scope may be set to ‘1’, or ‘Tenant’.
How do you know what search scope has been set?
The simplest way to see what search scope has been set is to right click on the front page and click on ‘Page Source’ or click CTRL-U. In the details that open on a new tab, search for searchScope. You should see two results: “searchScope”:0 and “searchScope\\\”:0. (‘0’ is the default.)
The other options you might see are: 1 – Tenant, 2 – Hub, 3 – Site.
How to change the search scope
To change the searchScope setting for a specific site, you need to have the the ability to run the following PowerShell PnP script, noting in this example that the searchScope is being changed to ‘1’ (Tenant).
You can also update every site in the tenant to the Tenant (‘1’) by running the following command (instead of ‘$web.SearchScope = 1’):
PS C:\WINDOWS\system32> Set-PnPSearchSettings -SearchScope Tenant
When you do this, the words ‘Search in SharePoint’ now appear in the search box for the specific site, or every site (depending on which of the two options above were used):
To confirm this outcome, view the Page Source for the site. The ‘searchScope’ should now be set to the option selected (in the case above, ‘1’).
What do searches retrieve now?
Once the search scope is changed to ‘1’, a search ‘Search in SharePoint’ from a site will return results from across the tenant.
This is the same outcome as when you click on ‘Organization’ from a site that has the default search scope ‘0’ or ‘3’ set.
How do you return the site to the default?
To set the site back to the default, run the same script above but change the searchScope to ‘0’.
To set every site back to the default, run this command:
PS C:\WINDOWS\system32> Set-PnPSearchSettings -SearchScope DefaultScope
(alternatives are Hub, Site, Tenant)
To confirm this outcome, view the Page Source for the site. The ‘searchScope’ should now be set to the option ‘0’.
Could ‘Search this site’ return Tenant results?
In some cases the Search bar query might say ‘Search this site’ but the search query returns tenant results. If this happens there may be a conflict.
If there is a requirement to change the default text (‘Search this site’ or ‘Search in SharePoint’), run the following command, noting that it requires the ability to run custom scripts on the site in question as the tenant administrator, or for all sites:
Set-PnPSearchSettings -Scope Site -SearchBoxPlaceholderText “Text in search box“
(Where ‘Site’ = the site invoked by the PnP command, and ‘Web’ = all sites).
Managing the search box in SharePoint sites | Microsoft Docs
Configure modern search results to search all of your organization (rather than the current site) – Microsoft Tech Community
Feature image source, used with permission of the author: https://www.foolishbricks.com/day-276-the-needle-in-the-haystack/