Removing a Site Collection from Search Results in SharePoint Online
One of my customers presented me with an interesting scenario recently, their security team were looking to define a process to take a SharePoint Online Site Collection temporarily offline. This is easy enough to achieve using the PowerShell cmdlet Set-SPOSite (example below).
- Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking -ErrorAction SilentlyContinue
- Connect-SPOService -Url https://tenant-admin.sharepoint.com -credential email@example.com
- Set-SPOSite -Identity https://tenant.sharepoint.com/sites/site -LockState NoAccess
However, there is still a chance that items contained within the Site Collection are returned in search results which could disclose some of the content (although the items themselves wouldn't be accessible as the site is locked).
We took a look at the Search Results Removal feature – Admin Center > Admin Centers > SharePoint > Search > Remove Search Results. However, this feature is for removing specific items from the search index rather than an entire Site Collection. This would be perfect if needed to remove a specific document from the search index, unfortunately in this case we needed to remove an entire Site Collection.
The approach that we eventually took was to use a Query Rule. This doesn’t actually remove the Site Collection from the search index, it merely adapts the search query to omit any results for content within the Site Collection so isn't 100% foolproof and can be worked around. If you do need to block a Site from being crawled you can do this via Search and Offline Availability in Site Settings.
In order to create a Query Rule to omit the Site from search results, do the following:
- Navigate to the Search Center Site Collection within your tenant (if you'd like to enforce this tenant-wide rather than this specific Search Center you will need to do this via SharePoint Admin Center > Search > Manage Query Rules)
- Select Site Settings
- Select Search Query Rules within the Site Collection Administration section
- From the Select a Result Source… dropdown select Local SharePoint Results
- Click New Query Rule
- Give it an appropriate name
- Within the Query Conditions section click Remove Condition
- Within the Actions section select Change Ranked Results by Changing the Query
- Append the following in the Query text box (updating the URL to that of the Site Collection to omit from Search Results) -path:https://tenant.sharepoint.com/sites/HR. In my example I have excluded the HR Site Collection.
- Click OK and then Save
If you now submit a search query via the Search Center, you should notice that any results from this Site Collection will be omitted. If you then need to permit search results from the Site Collection you can either delete the Query Rule or make it inactive (via the rule dropdown).
This approach will also work with SharePoint 2013 and 2016.
Brendan Griffin - @brendankarl