Skip to content

HubSite

Planning

Integration

Tips

-Introducing the idea of Archive Hub Sites - How to get the hubId in SharePoint Framework - Fix broken Hub Sites in SharePoint Online

cls

$tenantAdmin = "https://contoso-admin.sharepoint.com"
$hubSite = "https://contoso.sharepoint.com/sites/hub"
$extensionGUID = "6da1a9e8-471d-4f39-80e6-a7ded02e8881"
$extensionName = "Extension Name"
$extensionTitle = "Extension Title"

try
{
Connect-PnPOnline -Url $tenantAdmin -UseWebLogin
} catch {
Write-Host "Unable to connect."
exit
}

$HubSite = Get-PnPHubSite $hubSite
$HubSiteId = $HubSite.SiteId
$ModernSites = (Get-PnPTenantSite -Template 'GROUP#0') + (Get-PnPTenantSite -Template 'SITEPAGEPUBLISHING#0')
$SitesFromHub = New-Object System.Collections.ArrayList

Write-Host ("Searching {0} sites:" -f $HubSite.Title) -BackgroundColor Gray
foreach ($ModernSite in $ModernSites){
    $site = Get-PnPHubSite $ModernSite.Url
    if($site.SiteUrl){
    if($site.SiteId -eq $HubSiteId){
            Write-Host ("* {0} - {1}" -f $ModernSite.Title, $ModernSite.Url)
            $SitesFromHub.Add($ModernSite) | Out-Null
        }
    }
}

Write-Host ""
Write-Host "Installing at:" -BackgroundColor Gray
foreach ($SiteHub in $SitesFromHub){
    Write-Host ("* {0} - {1} ... " -f $SiteHub.Title, $SiteHub.Url) -NoNewline
    Connect-PnPOnline -Url $SiteHub.Url -UseWebLogin
    Add-PnPCustomAction -ClientSideComponentId $extensionGUID -Name $extensionName -Title $extensionTitle -Location ClientSideExtension.ApplicationCustomizer -Scope site
    Write-Host "Done" -BackgroundColor Green
    Disconnect-PnPOnline
}

Write-Host "All Done"

CSOM

Search API

 # List all sites being a hub site or associate to a hub site
$results = Submit-PnPSearchQuery -Query 'contentclass=sts_site' -RefinementFilters 'departmentid:string("{*",linguistics=off)' -TrimDuplicates $false -SelectProperties @("Title","Path","DepartmentId","SiteId") -All -RelevantResults

# Filter out the hub sites
$hubSites = $results |? { $_.DepartmentId.Trim('{','}') -eq $_.SiteId  }

# Loop over the hub sites
foreach( $hub in $hubSites ) {
    Write-Host $hub.Title - $hub.Path -ForegroundColor Green
    # Filter out sites associated to the current hub
    $associatedSites = ($results |? { $_.DepartmentId -eq $hub.DepartmentId -and $_.SiteId -ne $hub.SiteId })
    foreach($site in $associatedSites) {
        Write-Host "`t"$site.Title - $site.Path -ForegroundColor Yellow
    }
}