kwizcom banner advertisement

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 new managed property in SharePoint Online, We had to Reindex the SharePoint Online site. Typically, whenever you do changes to the search schema, You should Reindex the site. Unlike SharePoint On-premises, where we use 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've to go to each site and manually trigger the crawl to accomplish the same task.
  • Go to Site Settings >> Under "Search" heading, Click on Search and Offline Availability
  • On Search and offline Availability page, Under Reindex section, click on 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 SharePoint Online site, there is a property called "vti_searchversion" gets incremented to instruct SharePoint to denote the site needs to be reindexed. So, lets use PowerShell to do the same.
#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://crescenttech.sharepoint.com/"

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

PowerShell to Reindex SharePoint Online Site Collection
Lets change the script a bit to iterate through all sites within a site collection to trigger re-indexing for the whole 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://crescenttech.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 reindex in next crawl.
SharePoint Online: How to Trigger Full Reindex using PowerShell? SharePoint Online: How to Trigger Full Reindex using PowerShell? Reviewed by Salaudeen Rajack on February 14, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.