Upgrading the PnP Modern Search Web Parts from v3 to v4: Where are they?

My frustration last week in my post Dear Microsoft: Determining Solution Usage in Your Tenant Shouldn’t Be Like This was driven by a need to understand where the PnP Modern Search Web Parts had been used in pages. Now that v4 is available and solid, I want to discuss upgrading the v3 Web Parts to v4 with my clients. Generally, these Web Parts don’t get used in a lot of places across a tenant, but one can’t always be sure.

If you haven’t caught on, I’m a huge fan of the PnP Modern Search Web Parts. If you haven’t looked at them to extend your use of search to create highly valuable solutions, you should.

I wrote a little PowerShell to help me with this. All I really need is a list of the pages where the Web Parts are used; upgrading is a manual (but usually not horrible) process.

While there still isn’t a pages API, Mikael Svenson (@mikaelsvenson) suggested a method using search – as he is want to do. Turns out in this case, it works well. As Mikael mentioned in his post I linked to in my previous post, the trick is in knowing the GUID(s) for the Web Part(s) you want to locate across pages in your tenant.

This PowerShell could easily be reworked to find any Web Part(s) across your tenant. In this case, I want to carve things up site by site so I know who to talk to about upgrading the Web Parts in the pages. Other times, you might just want the list of occurrences without worrying about the specific sites so much.

The only sort of nice trick I’m using is to search slightly differently in the root site so as not to double report the pages.

# ModernSearchWebPartsUpgradev3tov4.ps1 - Inventory PnP Modern Search Web Parts usage to upgrade from v3 to v4

# Connect to your tenant here. This should be the only change you need to make to use this script.
Connect-PnPOnline -Url https://sympmarc-admin.sharepoint.com -UseWebLogin

# You can get the Ids for the v3 Web Parts by adding them to a page and running:
# Get-PnPClientSideComponent -Page "page-name" | Where-Object { Title -ne $null } | Select-Object Title, WebPartId

# Title            WebPartId
# -----            ---------
# Search Filters   e899ac12-9256-4c8d-a8ad-dbd20fc459c3
# Search Box       096b96cc-8a44-41fa-9b4d-c0ab2ab2a779
# Search Verticals 9d441773-f735-46a3-9ca0-9e2eb4bef203
# Search Results   42ad2740-3c60-49cf-971a-c44e33511b93

$webPartIds = @(
    "e899ac12-9256-4c8d-a8ad-dbd20fc459c3",
    "096b96cc-8a44-41fa-9b4d-c0ab2ab2a779",
    "9d441773-f735-46a3-9ca0-9e2eb4bef203",
    "42ad2740-3c60-49cf-971a-c44e33511b93"
    )

# Get all the sites in the tenant
$sites = Get-PnPTenantSite

# You may choose to include/exclude some subsets of sites
$filteredSites = $sites #| Where-Object { $_.Url -eq 'https://sympmarc.sharepoint.com/sites/DemoSite' } 

foreach ($site in $filteredSites) {

    # Build the query
    if($site.Url | Select-String "/sites/") {
        $query = "Path:$($site.Url) AND FileExtension:aspx AND ($($webPartIds -join " OR "))"
    } else {
        # Exclude /sites/* if looking in the root site
        $query = "Path:$($site.Url) -Path:$($site.Url)/sites/ AND FileExtension:aspx AND ($($webPartIds -join " OR "))"
    }
    #Write-Host -BackgroundColor White -ForegroundColor Black "Looking in $($site.Url)" # $($query)"

    # Submit the query
    $pages = Submit-PnPSearchQuery -Query $query -All -RelevantResults -ErrorAction SilentlyContinue | Select-Object OriginalPath 
    # If there are results, display them
    if ($pages) {
        Write-Host -BackgroundColor White -ForegroundColor Black "Found v3 Web Parts in this site: $($site.Url)" # $($query)"
        foreach ($page in $pages) {
            Write-Host -BackgroundColor Green -ForegroundColor Black ">>> Found Web Parts in this page: $($page.OriginalPath)"                
        }
    }

}

The output of the script will look something like this. It’s not beautiful, but it gets the job done.

If you’d like to download the full script, I’ve stored it in my marc-demos repo on Github. I’ll make updates here and there, if needed. Happy upgrading!

p.s. We still need a pages API.

Similar Posts

2 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.