Yes/No Column PnP PowerShell

In this blog post, we will learn, in SharePoint Online, what a hub site is, what its advantages are, and how to create one using PnP PowerShell. In addition, we will also see how a site collection can be associated with the hub site.

What is Hub Site in SharePoint Online?

Hub sites are used in SharePoint Online to create relationships between various site collections to better organise them. These site collections are connected to one another in accordance with a number of variables, including department, market, division, etc. They serve as the “connective tissue” that connects groups of team sites and communication sites together, according to Microsoft.

This idea is based on one of the core concepts of a modern intranet utilising Microsoft SharePoint, which is that each unit of work should have its own site collection to better manage governance and growth over time. In SharePoint Online, a site collection is created, registered as a Hub site in your tenant, and then connections to other site collections are made. We’ll discuss how PnP PowerShell can be used to do this. However, let’s first explore the benefits of using hub sites.

Features/Benefits of Hub Site

There are lot of benefits of using the hub site and few of them are listed below:

  1. Consistent look and feel
    • All the site collections associated with the hub sites, get the common navigation which is also called as the Global navigation. In addition to this, each site collection also inherits the same branding (logo, theme, etc.) applied to the hub site.
  2. Search
    • While searching within the hub, user can access content from any of the connected site based on the permissions.
  3. Security / Permissions
    • Hub Site Owners can use the same permissions on all other associated sites which are configured on the hub site. By default, this is not enabled but owners have option to enable the hub site permission sync.
  4. Web Parts
    • In this kind of setup, few web parts can be very useful. Such as News Web Part, you can publish a news on the hub site and any associated site can see that news if the web part is added to that site collection’s home page.
    • Another example is Events Web Part, which can help you to show events on all the associated site collections.

Setup Hub Site Collection

Now, let’s look at the PnP PowerShell code to setup a hub site in a tenant:

Note: In case you need any help to setup the PnP PowerShell in your machine, check my another blog post available at https://www.hemantkabra.in/microsoft-sharepoint/pnp-powershell-how-to-get-started/ for step by step instructions.

# Tenant Url
$TenantUrl = "https://hemantkabra.sharepoint.com"

Write-Host "Conncting to the Tenant - $($TenantUrl)"

# Connect to the SharePoint Tenant using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $TenantUrl -Interactive

Write-Host "Connected to the Tenant - $($TenantUrl)" -ForegroundColor Green

# Hub Site Url that needs to be created
$SiteUrl = "https://hemantkabra.sharepoint.com/sites/HubSite"

# Setup the Site Title
$SiteTitle = "HK Hub Site"

# Setup the Email ID Of the Owner
$SiteOwner = "hemant.kabra@hemantkabra.in"

# Create the Site Collection using New-PnPSite cmdlet
Write-Host 'Creating Site Collection..!'
$SiteCreated = New-PnPSite -Type CommunicationSite -Title $SiteTitle -Url $SiteUrl -Owner $SiteOwner -Wait
Write-Host 'Site Collection Created successfully..!' -ForegroundColor Green
    
# Once site collection is created, we need to register it as a Hub Site
# Let's register the Hub Site in tenant using Register-PnPHubSite cmdlet
Write-Host "Registering the $($SiteUrl) Site as hub site"
$RegisteredHubSite = Register-PnPHubSite -Site $SiteUrl -Principals $SiteOwner
Write-Host "Registered the $($SiteUrl) Site as hub site in $($TenantUrl) Tenant" -ForegroundColor Green

# Disconnect the Connection
Disconnect-PnPOnline

Once the hub site is created, you can verify it using the below cmdlet:

Get-PnPHubSite -Identity "https://hemantkabra.sharepoint.com/sites/HubSite"

This will return the properties of the specified hub site.

Associate a Site Collection with Hub Site

Once the Hub Site is created and registered as a Hub Site in the tenant, it is ready to associate other site collections with it. For this, first of all we will need the Hub Site Id. We can get the Hub Site Id using the below mentioned Rest API:

https://<Hub Site Collection URL>/_api/site?$select=IsHubSite,HubSiteId

Once you have the Hub Site Id, you can use the below code to create a site collection and associate it with the hub site:

# Tenant Url
$TenantUrl = "https://hemantkabra.sharepoint.com"

# Site Url that needs to be associated with the Hub Site
$SiteUrl = "https://hemantkabra.sharepoint.com/sites/AssociatedChildSite"

# Hub Site ID
$HubSiteId = "b402ca37-a958-471a-8576-86909edd5f77"

Write-Host "Conncting to the Tenant - $($TenantUrl)"
# Connect to the SharePoint Tenant using Connect-PnPOnline cmdlet
Connect-PnPOnline -Url $TenantUrl -Interactive
Write-Host "Connected to the Tenant - $($TenantUrl)" -ForegroundColor Green

# Setup the Site Title
$SiteTitle = "HK Associated Site"

# Setup the Email ID Of the Owner
$SiteOwner = "hemant.kabra@hemantkabra.in"

# Create the Site collection and associate it with hub site
Write-Host 'Creating Site Collection..!'
$SiteCreated = New-PnPSite -Type CommunicationSite -Title $SiteTitle -Url $SiteUrl -HubSiteId $HubSiteId -Owner $SiteOwner -Wait
Write-Host 'Site Collection Created successfully..!' -ForegroundColor Green

# Disconnect the Connection
Disconnect-PnPOnline

Let’s take another scenario where you already have a site collection and you are looking to associate it with a hub site. See below code for this scenario,

# This will add the specified site to the hub site
Add-PnPHubSiteAssociation -Site "https://hemantkabra.sharepoint.com/sites/mysite" -HubSite "https://hemantkabra.sharepoint.com/sites/HubSite"

Official Documentation

In this article, we are using below mentioned cmdlets from the PnP PowerShell and you can check their official documentation for more details.

I hope you enjoyed reading this blog post. Please feel free to leave a comment in case you have any feedback, suggestions, or queries.

Related Posts

Leave a Reply

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