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

6 Comments

  1. What happens if you upgrade the existing pnp-modern-search-parts-v4 to the newer version of pnp-modern-search-parts-v4?

    On a Dev Tenant I have done so but can’t replicate the situation for a customer. The settings seem the same, they didnt get deleted or lost. There are some new options to select in the settings. But I’m sure in the code a lot more has changed.

    How do you update it in general? Remove the webparts and reconfigure after updating? Or just updating and see what the effect is on the running site?

      1. There is not really an issue. The older version of V4 might be causing issues on $customer Tenant. In developer options in webbrowser we see errors in the console. Some about code being decrepated. In github support page I’ve got the advice to update the version to the current one.

        We don’t have a test site for the site that has those webparts. We dont have experience with updating the webpart since a third party had installed this for the customer.

        So we are just checking and exploring what happens when you update the older version v4 to the most current version. But it seems you are saying that after uploading the new SPPKG file everything stays the same, all settings applied, same functionatility, just some extra settings available?

        1. Got it. I’ve found updating v4 is almost always painless. It could depend on how far you need to jump, but I’d just do it and just retest the functionality.

          If you see problems, you can revert to the old version if you need to, but I wouldn’t do that unless you have major problems. Instead fix any issues you find, which are likely to be minimal.

          You could also set up a site-level app catalog to test in one site first if you’re worried about it.

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.