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 tenantadmin@tenant.onmicrosoft.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.

SearchResultsRemoval

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:

  1. 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)
  2. Select Site Settings
  3. Select Search Query Rules within the Site Collection Administration section
  4. From the Select a Result Source… dropdown select Local SharePoint Results
  5. Click New Query Rule
  6. Give it an appropriate name
  7. Within the Query Conditions section click Remove Condition
  8. Within the Actions section select Change Ranked Results by Changing the Query
  9. 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.
  10. Click OK and then Save

QueryRule

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


Comments (5)

  1. P_rashant says:

    great article brendan….

  2. Sasha says:

    Brendan,

    This was helpful. However – when trying to remove multiple site collections via query a character limit appears. This also happens when you try to break out into several queries. Have you been able to successfully limit several site collections? We are trying to remove about 70.

  3. NB says:

    Thanks it helped.

Skip to main content