SharePoint Online: How to Exclude a Site from Search Results?
Requirement: Exclude a Site from Search in SharePoint Online.
How to exclude a SharePoint Online Site from Search?
SharePoint Online search results are security trimmed by default, and users get search results from all sites to which they have access. However, there are some scenarios where you may want to prevent a site from appearing 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. In this post, we will discuss how to exclude a site from search results in SharePoint Online.
- Open the SharePoint Online site which you want to prevent from appearing 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.
- Set the “Allow this site to appear in search results?” option to “No” in the Indexing Site Content section, Then click on OK.
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 the 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 excludes site collection from search. Make sure you connect to the site collection (not the subsite!).
Exclude Site from Search Results using Query Rules
Here is an alternate approach to exclude content from the 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 on the 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/*
- 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/*
You can also remove existing search results from the search index with the “Remove Search Results” page in the SharePoint Admin center! How to Remove Search Results from SharePoint Online?
Conclusion:
Excluding a site from search results in SharePoint Online is a simple process that can be accomplished by editing the site’s crawl setting on the SharePoint Search Administration page. By setting the site’s crawl setting to “No”, we can prevent the site from appearing in search results. You can also set this property using PowerShell to automate the process. This can be useful for sites that contain sensitive or confidential information, or for sites that are not relevant to the majority of users. Keep in mind that it may take some time for the changes to take effect.
How can we exclude multiple sites using Powershell script?
Does this also work for the modern sharepoint experience (Microsoft Search)?
THANK YOU!!!! This helped me so much with one of my queries you’re the best.
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
Query rule text with “-Path:Test123” should fulfill your requirements!