SharePoint Online: How to Exclude Site from Search Results?

Requirement: Exclude Site from Search in SharePoint Online

How to Exclude a SharePoint Online Site from Search?
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 Settings Icon and then choose "Site Settings"
  • In 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 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 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 "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 "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/*
SharePoint Online: How to Exclude Site from Search Results? SharePoint Online: How to Exclude Site from Search Results? Reviewed by Salaudeen Rajack on March 23, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.