SharePoint Online: How to Exclude Site from Search Results?

Requirement: Exclude Site from Search in SharePoint Online

SharePoint Online search results are security trimmed by default. Users get search results from all sites where they have access. However, there are some scenarios where you may want to prevent a site to appear in search results, E.g. We’ve got a document archive site collection for backup purposes, and we don’t want results from this site to appear in global search results.

  • Open the SharePoint Online site which you want to prevent to appear in the search result. 
  • Click on the Settings Icon and then choose “Site Settings”
  • On the Site Settings page, click on the “Search and offline availability” link under the “Search” section.
    sharepoint online exclude site from search
  • Set the “Allow this site to appear in search results?” option to “No” in the Indexing Site Content section, Then click on OK.
    sharepoint online exclude site collection from search

These changes take effect after the next crawl – The site and all contents from it will not appear in the search result. 

SharePoint Online: Exclude Site from Search using PowerShell

To exclude a SharePoint Online site collection from the search index, use this PowerShell script:

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing/2018"

#Get Credentials to connect
$Cred= Get-Credential
  
Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()

    If($Web.NoCrawl)
    {
        Write-host -f Yellow "Site is Already excluded from Search Index!"
    }
    Else
    {
        #Exclude Site from Search
        $Web.NoCrawl = $True
        $Web.Update()
        $Ctx.ExecuteQuery()
        Write-host -f Green "Site is Excluded from Search Index Successfully!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Exclude a Site collection from Search in SharePoint Online

We can also use PnP PowerShell to exclude a site from SharePoint Online search index.

#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Exclude Root Web of the Site Collection from Search Index
$Web = Get-PnPWeb
$Web.NoCrawl = $true
$Web.Update()
Invoke-PnPQuery

This exclude site collection from search. Make sure you are connecting to the site collection (not sub-site!).

Exclude Site from Search Results using Query Rules

Here is an alternate approach to exclude content from search using query rules:

  • Navigate to your search center. Typically, at: https://YourTenant.sharepoint.com/search/
  • Go to the Search Results page by entering some search query and clicking on search. 
  • Once You are in Search results page (Pages/results.aspx), Click on Settings gear >> choose “Edit Page”.
  • Locate the “Search Results” web part, and choose “Edit this web part” from its context menu. This opens the web part properties pane. 
  • In the Web part properties panel, click on the “Change Query” button. You’ll get a Popup page to build your query.
  • In the “Build your query” dialogue window, select “Path” in the property filter dropdown. Change the parameter to “Not starts with”. For the value, select “Manual value” and then type the URL for the site you want to exclude and then click the button “Add property filter”. This appends the “Query Text” box with -Path:https://site-to-remove/*. E.g: {searchboxquery} -Path:https://crescent.sharepoint.com/sites/archive/* 
    sharepoint online exclude from search results
  • Save, Check-In and Publish the page!If you want to exclude multiple site collections, use OR operator. E.g. {searchboxquery} -Path:https://crescent.sharepoint.com/sites/archive/* OR -Path:https://crescent.sharepoint.com/sites/backup/*

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

2 thoughts on “SharePoint Online: How to Exclude Site from Search Results?

  • September 9, 2020 at 8:28 PM

    In my tenant there is folder called “Test123” in all the SharePoint online sites(Approx 500 sites), I would like to exclude the particular folder from search results and index in SharePoint online. Is there any direct way to achieve this requirement

    Reply

Leave a Reply