Configure

SharePoint Online Hub Site Association

This script sample shows how to associate SharePoint online site with a hub site.

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

powershell

$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

Contributors

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.

Tool documentation & references

Keep exploring