Blog about anything related to my learnings
  • About
  • posts
bluesky
Blog about anything related to my learnings
POSTS

#connection details
$clientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path

$csvPath = $directorypath + "\TeamsChannelsExport.csv" # CSV should have a column 'SiteUrl'
# Read sites from CSV
$sites = Import-Csv -Path $csvPath
#$TenantName = "contoso"
#$adminSiteURL = "https://$TenantName-Admin.SharePoint.com"
#Connect-PnPOnline -Url $adminSiteURL -ClientId $clientId

function Add-ChannelNavigationLink {
    param(
        [string]$teamName,
        [string]$channelName,
        [string]$hubSiteUrl,
        [string]$channelSiteURL,
        [string]$teamSiteUrl,
        [string]$navParent
    )
    Log-Message "Adding site to hub navigation - $($channelSiteURL)" "Info"
    if($hubSiteUrl)
    {
        try {
           # if(!$pnpHubConnection)
            #{
               # $pnpHubConnection = Connect-PnPOnline -ClientId $clientId -CertificatePath $CertPath -CertificatePassword $CertPassword -Url $hubSiteUrl -Tenant "$($TenantName).onmicrosoft.com" -ReturnConnection
                $pnpHubConnection = Connect-PnPOnline -ClientId $clientId  -Url $hubSiteUrl  -ReturnConnection
                #}
            # Check if parent navigation node already exists
            $CurrentNavigation = Get-PnPNavigationNode -Location TopNavigationBar -Connection $pnpHubConnection

            $teamNode = $null
                #check if the parent node exists
                $parentNode = $null
                if($navParent)
                {
                    $parentNode = $CurrentNavigation | Where-Object { $_.Title -eq $navParent}
                    if(!$parentNode)
                    {
                        $parentNode = Add-PnPNavigationNode -Location TopNavigationBar -Title $navParent -url "http://linkless.header/"  -Connection $pnpHubConnection
                         Log-Message "Added Parent Node: $($navParent) to Hub navigation" "Info"
                    }
                }
                
                ###new code
                Log-Message "Adding team site: $($teamName) to Hub navigation" "Info"
                # Find the correct position to insert alphabetically
                $preNode = $null
                if($parentNode)
                {
                    Get-PnPProperty -ClientObject $parentNode -Property Children -Connection $pnpHubConnection| Out-Null
                    $teamNode = $parentNode.Children| Where-Object { $_.Title -eq $teamName}
           
                    if(!$teamNode){
                        Log-Message "Team site not found in navigation: $teamName" "Info"
                    
                      $preNode =  $parentNode.Children | Where-Object { $_.Title -lt $teamName } | Select-Object -Last 1 -ErrorAction SilentlyContinue
                      if ($preNode) {
                        $teamNode = Add-PnPNavigationNode -Location TopNavigationBar -Title $teamName -Url $teamSiteUrl -PreviousNode $preNode.Id -Parent $parentNode.Id -OpenInNewTab -Connection $pnpHubConnection
                       }
                       else {
                        $teamNode = Add-PnPNavigationNode -Location TopNavigationBar -Title $teamName -Url $teamSiteUrl  -First -Parent $parentNode.Id -OpenInNewTab -Connection $pnpHubConnection
                       }
                    }
                }
                else {
                   
                    $teamNode = $CurrentNavigation | Where-Object { $_.Title -eq $teamName}
                    if(!$teamNode){
                        Log-Message "Team site not found in navigation: $teamName" "Info"
                      $preNode = $CurrentNavigation | Where-Object { $_.Title -lt $teamName } | Select-Object -Last 1 -ErrorAction SilentlyContinue
                      if ($preNode) {
                        $teamNode = Add-PnPNavigationNode -Location TopNavigationBar -Title $teamName -Url $teamSiteUrl -PreviousNode $preNode.Id  -OpenInNewTab -Connection $pnpHubConnection
                       }
                       else {
                        $teamNode = Add-PnPNavigationNode -Location TopNavigationBar -Title $teamName -Url $teamSiteUrl  -First -OpenInNewTab -Connection $pnpHubConnection
                       }
                    }
                }
            
             
          #  Log-Message "Adding Channel site: $($channelName) to Hub navigation" "Info"
          if($channelName -ne "General"){
            $channelNodeExists = $null
            Get-PnPProperty -ClientObject $teamNode -Property Children -Connection $pnpHubConnection| Out-Null
            if ($teamNode.Children) {
                $channelNodeExists = $teamNode.Children | Where-Object { $_.Url -eq $channelSiteURL }
            }
 
            if (!$channelNodeExists) {
                # Add channel navigation link in alphabetical order
                if (!$teamNode.Children) {
                    # No existing children, add as first child
                    Add-PnPNavigationNode -Location TopNavigationBar -Title $channelName -Url $channelSiteURL -Parent $teamNode.Id -OpenInNewTab -Connection $pnpHubConnection | Out-Null
                }
                else {
                    # Find correct position alphabetically among existing children
                    $preNode = $teamNode.Children | Where-Object { $_.Title -lt $channelName } | Select-Object -Last 1 -ErrorAction SilentlyContinue
                    if ($preNode) {
                      Add-PnPNavigationNode -Location TopNavigationBar -Title $channelName -Url $channelSiteURL -Parent $teamNode.Id -PreviousNode $preNode.Id -OpenInNewTab -Connection $pnpHubConnection | Out-Null
                    }
                    else{
                       Add-PnPNavigationNode -Location TopNavigationBar -Title $channelName -Url $channelSiteURL -Parent $teamNode.Id -OpenInNewTab -Connection $pnpHubConnection -First  | Out-Null
                    }
                }
 
                Log-Message "Added channel navigation: $($channelName) under $($teamNode.Title)" "Info"
            }
            else {
                Log-Message "Channel navigation already exists: $($channelName)" "Info"
            }
         }
        }
        catch {
            Log-Message "Error adding channel navigation link for $($channelName)`: $($_.Exception.Message)" "Error"
        }
    }
    else {
        Log-Message "Hub site url not provided for '$($channelSiteURL)'" "Error"
    }
}


foreach ($site in $sites) {

if($site.ChannelName -eq "General")
{
    Add-ChannelNavigationLink -teamName $site.TeamName -channelName $site.ChannelName -hubSiteUrl $site.HubSiteUrl -teamSiteUrl $site.SiteUrl  -navParent "Projects"
}
else{
    Add-ChannelNavigationLink -teamName $site.TeamName -channelName $site.ChannelName -hubSiteUrl $site.HubSiteUrl -channelSiteURL $site.SiteUrl -navParent "Projects"
}
}
    © Blog about anything related to my learnings 2026
    bluesky