Configure

Reindex SharePoint sites

Keeping your SharePoint environment up-to-date is crucial, especially after making schema changes - this script streamlines the process of reindexing sites, libraries, or lists.

Summary

When using SharePoint columns in search solutions, you may need to map crawled properties to managed properties and update the search schema. For example, you can map the crawled property ows_Foo to RefinableString100 and use it as a filter in KQL. For more information, refer to How Do Site Columns Become Managed Properties.

If your schema change applies to existing content, you must schedule a re-index operation for the affected content in order to have the data populated into the search index.

This PnP PowerShell script streamlines the process of re-indexing sites, libraries, or lists to ensure that search results remain accurate and relevant.

powershell
#Prompt for caution
Read-Host -Prompt "Are you sure you know what you are doing, and have read the text at https://pnp.github.io/script-samples/spo-reindex-sites/README.html?"

#Set Parameters
$AdminCenterURL = "https://contoso-admin.sharepoint.com"

# Connect to SharePoint admin center
Connect-PnPOnline -Url $AdminCenterURL -Interactive

#Get SharePoint sites
$m365Sites = Get-PnPTenantSite -Detailed | Where-Object {($_.Url -like '*/teams-*' -or $_.Template -eq 'TEAMCHANNEL#1') -and $_.Template -ne 'RedirectSite#0' } #filter to exclude redirect sites and to include team channel sites in the list
$m365Sites | ForEach-Object {
    # Connect to SharePoint site
    Connect-PnPOnline -Url $_.Url -Interactive
    
    #Request Reindex
    Request-PnPReIndexWeb
    Write-host "Reindexing: " $_.Url
}

# Disconnect SharePoint online connection
Disconnect-PnPOnline

Contributors

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Tool documentation & references

Keep exploring