SharePoint Online: Set Hub Site Association using PowerShell

How to Configure Hub Site Association in SharePoint Online using PowerShell?
Hub sites in SharePoint Online is a new way to organize related sites together into centralized portals based on organizational hierarchy. More on Hub site is How to Configure Hub Sites in SharePoint Online?

Set Hub Site Association in SharePoint Online using PnP PowerShell
To associate a site collection with an existing hub site, use:
#Config Variables
$TenantSiteUrl = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/Intranet"
$SiteURL = "https://crescent.sharepoint.com/sites/2020projects"

#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -Credentials (Get-Credential)

#Associate Site collection with HubSite
Add-PnPHubSiteAssociation -Site $SiteURL -HubSite $HubSiteURL

Set Hub Site Association for Site Collections from a CSV File: 
If you want to associate multiple sites with a hub site, just create a CSV file with Site URLs. Here is mine:
sharepoint online associate hub site powershell

To Associate multiple site collections from a CSV file to a hub site, you can use this script:
#Config Variables
$TenantSiteUrl = "https://crescent-admin.sharepoint.com"
$HubSiteURL = "https://crescent.sharepoint.com/sites/Intranet"
$CSVFilePath = "C:\Temp\Sites.csv"

#Connect to PnP Online
Connect-PnPOnline -Url $TenantSiteUrl -Credentials (Get-Credential)

#Get data from CSV File
$Sites = Import-Csv -Path $CSVFilePath 

#Associate each Site collection with HubSite
ForEach ($Site in $Sites)
{
    Add-PnPHubSiteAssociation -Site $Site.SiteURL -HubSite $HubSiteURL
    Write-host "Associated $($Site.SiteURL) with Hub Site"! -f Green
} 
Alright, How do I remove the hub site association from a site collection? Well, to remove the hub site association from a SharePoint Online site collection, use:
Remove-PnPHubSiteAssociation -Site “<Site URL>”

To retrieve all hub sites and its associated sites, use: Get SharePoint Online Hub Site Associations using PowerShell
SharePoint Online: Set Hub Site Association using PowerShell SharePoint Online: Set Hub Site Association using PowerShell Reviewed by Salaudeen Rajack on April 08, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.