SharePoint Online: Remove List from Search using PowerShell

By default, all SharePoint Online lists and libraries appear in search results. In some cases, you want to remove a particular list’s contents from appearing in search results. Say, You want to hide the image library’s content from appearing in search results. This blog post will show you how to remove a list from search results in SharePoint Online.

You can configure a specific list to be excluded from the search. To exclude a list or library from the search index using the user interface, go to :

  1. List Settings >> Click on “Advanced Settings” 
  2. Scroll down and under the Search section, select “Yes”  for “Allow items from this document library to appear in search results?” Option.
    sharepoint online remove list from search
  3. Click OK to commit changes.

This setting allows or disallows SharePoint Online crawlers to index the list. Wait for 4 to 6 hours, and this list should be removed from search results.

Remove Search Results from SharePoint Online

While the above steps prevent the SharePoint Online search crawler from indexing the specific list, what about the existing search results of the list indexed already? Well, you can remove the search results from SharePoint Online:

  1. Login to SharePoint Online Admin center >> Click on “More features”.
  2. Open Search >> On the search page, click on “Remove Search Results”.
  3. Enter the full URL(s) of the items you want to remove from the search results.
  4. Click on “Remove Now”.sharepoint online remove search results

How to Remove SharePoint Online List from Search using PowerShell?

Let’s use PowerShell to exclude the list or library from the SharePoint Online search.

#Load SharePoint CSOM 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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Documents"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the list
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    
    #Exclude from Search Results
    $List.NoCrawl = $True
    $List.Update() 

    $Ctx.ExecuteQuery() 

    Write-host "List has been excluded from Search Index!" -f Green
}
Catch {
    write-host -f Red "Error Excluding List from Search Index!" $_.Exception.Message
} 

Let’s make the script reusable by wrapping it inside a function. Set the “NoCrawl” parameter to “True” to remove a list from the search.

#Load SharePoint CSOM 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"

#Function to Enable or Disable Search Crawl
Function Set-SPOListCrawl()
{
    Param ([string]$ListName,[bool]$NoCrawl)

    Try {
        #Get the list
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
    
        #Enable/Disable List from Search Results
        $List.NoCrawl = $NoCrawl
        $List.Update() 

        $Ctx.ExecuteQuery() 

        Write-host "List Search Crawl Settings has been Updated!" -f Green
    }
    Catch {
        write-host -f Red "Error Setting List Search Crawler Settings!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Call the Function to Exclude a list from Search Crawl
Set-SPOListCrawl -ListName "Documents" -NoCrawl $True

This removes the content of the list or library from search results. Here is another article on excluding a list or library from the search index in SharePoint on-premises: How to Exclude SharePoint Site or List from Search Index?

SharePoint Online: Exclude List from Search using PnP PowerShell

Here is how to use PnP PowerShell to remove a list from search results:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Team Documents"

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

#Get the Context
$Context = Get-PnPContext
 
#Get the List
$List = Get-PnPList -Identity $ListName

If($List)
{
    #Set "NoCrawl" flag to exclude list from search
    $List.NoCrawl = $True 
    $List.Update() 
    $Context.ExecuteQuery()
    Write-host -f Green "List Excluded from Search Index Successfully. Changes takes effect after the next crawl!"
}
else
{
    Write-host -f Yellow "Could not find List '$ListName'"
}

To remove a SharePoint Online site from search results, use: How to Remove a Site from Search Results in SharePoint Online?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *