In SharePoint Online we do not have the option, to trigger reindexing the complete site collection. The reindex request must be send for each web in the site collection. The following script could be used, when the whole site collection needs to be reindexed.

#
# Parameters
# ==========
# 
# Url : root url of the site collection
# Credentials : username and password to connect with
#

param (
    [string]$Url,
    $Credentials
)

function Iterate-WebCollection
{
   param (
       $WebCollection
   )

    foreach ($web in $WebCollection)
    {
        $webUrl = $web.Url

        Write-Host -NoNewline $webUrl " ... "

        Connect-PnPOnline -Url $webUrl -Credentials $Credentials

        Request-PnPReIndexWeb

        Write-Host -ForegroundColor Green "Done."

        $CurrentWeb = Get-PnPWeb -Includes Webs

        Iterate-WebCollection -WebCollection $CurrentWeb.Webs

        Connect-PnPOnline -Url $webUrl -Credentials $Credentials
    }
}

if ($Credentials -eq $null)
{
    $Credentials = Get-Credential
}

Connect-PnPOnline -Url $Url -Credentials $Credentials

$web = Get-PnPWeb -Includes Webs
$webUrl = $web.Url

Write-Host -NoNewline $Url " ... "

Request-PnPReIndexWeb

Write-Host -ForegroundColor Green "Done."

Iterate-WebCollection -WebCollection $web.Webs

Advertisements