SharePoint Online: How to Trigger Full Reindex using PowerShell?

Requirement: Reindex SharePoint Online site using PowerShell.

How to Reindex the SharePoint site?

After mapping a crawled property to a new managed property in SharePoint Online, We had to Reindex the SharePoint Online site. Typically, whenever you make changes to the search schema, You should Re-index the site. Unlike SharePoint On-premises, where we use the Search Service application to trigger a full crawl, We can’t force a full crawl through the admin user interface in SharePoint Online. Instead, we must go to each site and manually trigger the crawl to ensure all content is fully searchable. This article will explore how to reindex a SharePoint Online site using site settings and PowerShell.

To reindex a SharePoint Online site, do the following:

  1. Go to Site Settings >> Under the “Search” heading, Click on Search and Offline Availability.
  2. On the the Search and offline Availability page, under the Reindex section, click the “Reindex” button and confirm the prompt.
    Reindex Site in SharePoint Online using PowerShell
Please note, We don’t have any control over the crawl schedules! SharePoint Online targets between 15 minutes and an hour for the time between upload and availability in search results. In cases of heavy environment use, this time can increase up to six hours!

Reindex Site in SharePoint Online using PowerShell

Behind the scenes, when you click on Reindex from the SharePoint Online site, a property called “vti_searchversion” gets incremented to instruct SharePoint to denote the site needs to be reindexed. So, let’s use PowerShell to reindex the site in SharePoint Online.

#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 Reindex SharePoint Online Site (web)
Function Reindex-SPOWeb($WebURL)
{
    #Setup 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($WebURL)
    $Ctx.Credentials = $Credentials
    
    #Get the Search version from Property bag
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()

    [Int]$SearchVersion = 0
    #Get the Search Version Property - If exists
    If ($Web.AllProperties.FieldValues.ContainsKey("vti_searchversion") -eq $True)
    {
        $SearchVersion = $Web.AllProperties["vti_searchversion"]
    }
    #Increment Search Version
    $SearchVersion++
    $Web.AllProperties["vti_searchversion"] = $SearchVersion
    $web.Update()
    $Ctx.ExecuteQuery()

    Write-Host -f Green "Updated Search Version To: " $SearchVersion
}

#variable
$WebURL="https://Crescent.sharepoint.com/"

#Call the function to reindex
Reindex-SPOWeb -WebURL $WebURL

This marks the site for reindex in SharePoint Online.

PowerShell to Reindex SharePoint Online Site Collection

Let’s change the script a bit to iterate through all sites within a site collection to trigger re-indexing for all sites in a site collection on the next crawl cycle.

#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 Reindex SharePoint Online Site (web)
Function Reindex-SPOWeb($WebURL)
{
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = $Credentials
    
        #Get the Search version from Property bag
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.Load($Web.AllProperties)
        $Ctx.ExecuteQuery()

        [Int]$SearchVersion = 0
        #Get the Search Version Property - If exists
        If ($Web.AllProperties.FieldValues.ContainsKey("vti_searchversion") -eq $True)
        {
            $SearchVersion = $Web.AllProperties["vti_searchversion"]
        }
        #Increment Search Version
        $SearchVersion++
        $Web.AllProperties["vti_searchversion"] = $SearchVersion
        $web.Update()
        $Ctx.ExecuteQuery()

        Write-Host -f Green "Updated Search Version To '$SearchVersion' on "$WebURL
    
        #Process all subsites of the current site
        ForEach($SubWeb in $Web.Webs)
        {
            Reindex-SPOWeb -WebURL $SubWeb.URL
        }
    }
    Catch {
        write-host -f Red "Error Setting Reindex Flag!" $_.Exception.Message
    }
}

#Site collection URL
$WebURL="https://Crescent.sharepoint.com"

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

#Call the function to reindex
Reindex-SPOWeb -WebURL $WebURL

This PowerShell marks the SharePoint Online site for reindexing in the next crawl.

PnP PowerShell to Reindex SharePoint Online Site

Here is how to request reindex a SharePoint Online site using the PnP PowerShell cmdlet Request-PnPReIndexWeb:

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

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

#Get the Web 
$Web = Get-PnPWeb

#Request Reindex
Request-PnPReIndexWeb -Web $Web 

By reindexing your SharePoint Online site, you can ensure that your content is easily discoverable and accessible through search, helping to improve productivity and collaboration within your organization.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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