GitHub Icon Image
GitHub

Reindex Search index for lists where a specific term is used (when you have renamed the term)

Summary

Once in a while you might need to rename a term in the term store. This script will help you reindex all lists where the term is used.

Example Screenshot

  • PnP PowerShell

#this is just a sample. In a real world scenario you would probably want to run this using an App registration with the necessary permissions
# in order to be able to search all content in the tenant

#connect to the term store
$spAdminUrl = "https://[your tenant]-admin.sharepoint.com"
if(-not $SPAdminConn)
{
    $SPAdminConn = Connect-PnPOnline -Url $spAdminUrl -Interactive -ReturnConnection
}
$Output = @()
#get all hits on the term guid
$termId = "the guid of the term you want to search for"
$query = $termId
# find the unique LISTS where the term is used and reindex them
$hits = Invoke-PnPSearchQuery -Connection $SPOAdminConn -Query $termId -All -CollapseSpecification "ListId:1"

foreach($hit in $hits.ResultRows)
{
    try 
    {
        Write-Host "Term $($termId) is used in $($hit["SPWebUrl"]) - $($hit["Title"])"
        $siteUrl = $hit["SPWebUrl"]
        $listId = $hit["IdentityListId"]
        $siteConn = Connect-PnPOnline -Url $siteUrl -Interactive -ReturnConnection
        $list = Get-PnPList -Connection $siteConn -Identity $listId
        Write-Host "Reindexing $($list.Title) in $($siteUrl)"
        $myObject = [PSCustomObject]@{
            URL     = $siteUrl
            ListName = $list.Title
            Status = "Reindexed"
    
        }        
        $Output+=($myObject)
        Request-PnPReIndexList -Identity $list -Connection $siteConn
    }
    catch 
    {
        $myObject = [PSCustomObject]@{
            URL     = $siteUrl
            ListName = $list.Title
            Status = "Failed $($_.Exception.Message)"    
        }        
        $Output+=($myObject)
    }
}

$Output | Export-Csv -Path "C:\temp\ReindexResults.csv" -Encoding UTF8 -Delimiter "|" -Force

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?

Contributors

Author(s)
Kasper Larsen

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.

Back to top Script Samples
Generated by DocFX with Material UI