GitHub Icon Image
GitHub

Hub Site Association

Summary

This sample shows how to associate sharepoint online site with hub site.

Implementation

  • Open Windows PowerShell ISE
  • Create a new file
  • Write a script as below,
  • First, we will connect to Tenant admin site.
    • Then we read site url and hub site url from a user,
    • And then we will do hub site association.

Example Screenshot

  • PnP PowerShell
  • SPO Management Shell
  • CLI for Microsoft 365

$AdminSiteURL = "https://{domain}-admin.sharepoint.com/"
$Username = "USERID"
$Password = "********"
$SecureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force 
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

Function Login() {
    [cmdletbinding()]
    param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)     
    Write-Host "Connecting to Tenant Admin Site '$($adminSiteURL)'" -f Yellow   
    Connect-PnPOnline -Url $adminSiteURL -Credentials $Creds
    Write-Host "Connection Successful" -f Green 
}

Function HubSiteAssociation {  
    try {  
        #read site url from user  
        $SiteUrl = Read-Host 'Enter Site Url'  
        #read hub site url from user  
        $HubSiteUrl = Read-Host 'Enter Hub Site Url'         
        Write-Host "Connecting to SharePoint site..." -ForegroundColor Yellow  
        #connect to the SharePoint site  
        Connect-PnpOnline -Url $SiteUrl -Credentials $Creds     
        Write-Host "Associate with a hub site..." -ForegroundColor Yellow           
        #hub site association           
        Add-PnPHubSiteAssociation -Site $SiteUrl -HubSite $HubSiteUrl   
        Write-Host "Hub site association completed..." -ForegroundColor Yellow  
    }  
    catch {  
        Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red  
    }  
}


Function StartProcessing {
    Login($creds);
    HubSiteAssociation
}

StartProcessing

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


$AdminSiteURL = "https://{domain}-admin.sharepoint.com/"
$Username = "USERID"
$Password = "********"
$SecureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force 
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

Function Login() {
    [cmdletbinding()]
    param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)     
    Write-Host "Connecting to Tenant Admin Site '$($adminSiteURL)'" -f Yellow   
    Connect-SPOService -Url $adminSiteURL -Credential $Creds
    Write-Host "Connection Successful" -f Green 
}

Function HubSiteAssociation {  
    try {  
        #read site url from user  
        $SiteUrl = Read-Host 'Enter Site Url'  
        #read hub site URL from user  
        $HubSiteUrl = Read-Host 'Enter Hub Site Url'         
        Write-Host "Connecting to SharePoint site..." -ForegroundColor Yellow  
        #connect to the SharePoint site  
        Connect-PnpOnline -Url $SiteUrl -Credentials $Creds     
        Write-Host "Associate with a hub site..." -ForegroundColor Yellow           
        #hub site association           
        Add-SPOHubSiteAssociation $SiteUrl -HubSite $HubSiteUrl   
        Write-Host "Hub site association completed..." -ForegroundColor Yellow  
    }  
    catch {  
        Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red  
    }  
}


Function StartProcessing {
    Login($Creds);
    HubSiteAssociation
}

StartProcessing

Check out the SPO Management Shell to learn more at: Introduction SharePoint Online Management Shell | Microsoft Docs


Function Login() {
  Write-Host "Connecting to Tenant Site" -f Yellow   
  $m365Status = m365 status
  if ($m365Status -match "Logged Out") {
    m365 login
  }
  Write-Host "Connection Successful!" -f Green 
}

Function HubSiteAssociation {  
  try {  
    #read site url from user  
    $SiteUrl = Read-Host 'Enter Site Url'  
    #read hub site url from user  
    $HubSiteUrl = Read-Host 'Enter Hub Site Url'   
              
    #hub site association           
    Write-Host "Associate with a hub site..." -ForegroundColor Yellow    
    
    $hubSitesList = m365 spo hubsite list | ConvertFrom-Json
    $hubSite = $hubSitesList | Where-Object SiteUrl -Match $HubSiteUrl 
    m365 spo hubsite connect --url $SiteUrl --hubSiteId $hubSite.SiteId 

    Write-Host "Hub site association completed..." -ForegroundColor Yellow  
  }  
  catch {  
    Write-Host "Error in hub site association:" $_.Exception.Message -ForegroundColor Red  
  }  
}

Function StartProcessing {
  Login
  HubSiteAssociation
}

StartProcessing

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
Chandani Prajapati (https://github.com/chandaniprajapati)
Jasey Waegebaert

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI