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 and prevent it from being indexed and displayed 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.
How to remove a list from SharePoint Online Search?
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 :
- Navigate to the list >> Click on settings gear >> choose “List Settings”
- On the list settings page, click on “Advanced Settings”.
- Scroll down and under the Search section, select “Yes” for “Allow items from this document library to appear in search results?” Option.
- 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:
- Login to SharePoint Online Admin center >> Click on “More features”.
- Open Search >> On the search page, click “Remove Search Results”.
- Enter the full URL(s) of the items you want to remove from the search results.
- Click on “Remove Now”.
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
}
PowerShell script to Disable List or Document Library from SharePoint Online Search
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 -Interactive
#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'"
}
The list or document library you specified should now be excluded from the search results. It may take some time for the change to take effect, as search results are returned from the index. To remove a SharePoint Online site from search results, use: How to Remove a Site from Search Results in SharePoint Online?