Skip to content

Commit

Permalink
Merge pull request #668 from tmaestrini/main
Browse files Browse the repository at this point in the history
new sample to copy a hub navigation from a source site in SPO to any desired target hub site
  • Loading branch information
pkbullock authored Mar 5, 2024
2 parents fbe3b48 + 8ae720a commit d78a828
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
120 changes: 120 additions & 0 deletions scripts/spo-copy-hubsite-navigation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
plugin: add-to-gallery
---

# Copy a hub navigation from a source site to any desired target hub site

## Summary

This script copies a hub navigation from any hub site (source) to another hub site (target). Use this script to create a consistent hub navigation for all your sites in SharePoint Online.

Based on the navigation structure of any hub site of your choice – e.g. the hub navigation of your home site, which serves as a template - any desired hub navigation within your SharePoint Online site architecture can be created identically.

> [!NOTE]
> The deployment process is idempotent; each navigation is only deployed once and replaced in the target hub site when it is deployed again. You can start the copying process as often as you like without expecting any side effects!

# [PnP PowerShell](#tab/pnpps)

```powershell
Function Copy-Hubnavigation {
[cmdletbinding()]
param(
[Parameter(
Mandatory = $true
)][string]$SourceSiteRelativeUrl,
[Parameter(
Mandatory = $true
)][string]$DestinationSiteRelativeUrl
)
Function Get-ToplevelHubnavigation([PnP.PowerShell.Commands.Base.PnPConnection]$sourceSiteConn) {
try {
$hubsiteNavigation = Get-PnPNavigationNode -Location TopNavigationBar -Connection $sourceSiteConn
return $hubsiteNavigation
}
catch {
Write-Host " ✘ failed: $($_)" -ForegroundColor Red
}
}
Function New-HubnavigationElement([Object]$naviItem, [Object]$parentItem, [PnP.PowerShell.Commands.Base.PnPConnection]$destSiteConn, [PnP.PowerShell.Commands.Base.PnPConnection]$sourceSiteConn) {
# construct path based on given relative url of item
$naviItem.Url = $naviItem.Url -eq "http://linkless.header/" ? "http://linkless.header/" : ($naviItem.Url.StartsWith("https://") ? $naviItem.Url : "$($naviItem.Context.Url)$($naviItem.Url.TrimStart('/'))")
if ($null -ne $parentItem) {
$node = Add-PnPNavigationNode -Location TopNavigationBar -Title $naviItem.Title -Url $naviItem.Url -Parent $parentItem.Id -Connection $destSiteConn
}
else {
$node = Add-PnPNavigationNode -Location TopNavigationBar -Title $naviItem.Title -Url $naviItem.Url -Connection $destSiteConn
}
# handle child nodes (recursively)
if ($null -ne $naviItem.Children) {
foreach ($childNaviItem in $naviItem.Children) {
# get the details about the node:
$childNaviItem = Get-PnPNavigationNode -Id $childNaviItem.Id -Connection $connHubsiteSource
New-HubnavigationElement -naviItem $childNaviItem -parentItem $node -destSiteConn $destSiteConn
}
}
}
if ($null -eq $SourceSiteRelativeUrl) { throw "No Source Hubsite Url provided" }
if ($null -eq $DestinationSiteRelativeUrl) { throw "No Destination Hubsite Url provided" }
$connAdmin = Get-PnPConnection
$spoBaseUrl = $connAdmin.Url.Replace('-admin', '')
$spoUrlSource = "$($spoBaseUrl)$($SourceSiteRelativeUrl)"
$spoUrlDestination = "$($spoBaseUrl)$($DestinationSiteRelativeUrl)"
$connHubsiteSource = Connect-PnPOnline -Url $spoUrlSource -ReturnConnection -Interactive
$connHubSiteDest = Connect-PnPOnline -Url $spoUrlDestination -ReturnConnection -Interactive
Write-Host "⭐️ Site '$($DestinationSiteRelativeUrl)';"
try {
# Delete all existing nodes:
Remove-PnPNavigationNode -Force -All -Connection $connHubSiteDest
Write-Host "⎿ Copying consistent hub navigation from '$($connHubsiteSource.Url)': " -NoNewline
$navigation = Get-ToplevelHubnavigation -sourceSiteConn $connHubsiteSource
foreach ($naviItem in $navigation) {
# get the details about the node:
$naviItem = Get-PnPNavigationNode -Id $naviItem.Id -Connection $connHubsiteSource
New-HubnavigationElement -naviItem $naviItem -parentItem $null -destSiteConn $connHubSiteDest -sourceSiteConn $connHubsiteSource
}
Write-Host " ✔︎ Done" -ForegroundColor DarkGreen
}
catch {
Write-Host " ✘ failed: $($_)" -ForegroundColor Red
}
finally {
$connHubsiteSource = $null
$connHubSiteDest = $null
$connAdmin = $null
}
}
# First connect to admin site of your tenant; make sure you are an SPO Admin:
Connect-PnPOnline "https://[tenant]-admin.sharepoint.com" -Interactive
# Copy hub navigation from the soure hub site (here "/") to the destination hub site (here "/sites/LearningHub"):
Copy-Hubnavigation -SourceSiteRelativeUrl "/" -DestinationSiteRelativeUrl "/sites/LearningHub"
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
***

## Source Credit

Sample taken from [https://github.com/tmaestrini/easyProvisioning/](https://github.com/tmaestrini/easyProvisioning)

## Contributors

| Author(s) |
|-----------|
| [Tobias Maestrini](https://github.com/tmaestrini)|


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-copy-hubsite-navigation" aria-hidden="true" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions scripts/spo-copy-hubsite-navigation/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"name": "spo-copy-hubsite-navigation",
"source": "pnp",
"title": "Copy a hub navigation from a source site to any desired target hub site",
"shortDescription": "This script copies a hub navigation from any hub site (source) to another hub site (target)",
"url": "https://pnp.github.io/script-samples/spo-copy-hubsite-navigation/README.html",
"longDescription": [
"This script copies a hub navigation from any hub site (source) to another hub site (target). Use this script to create a consistent hub navigation for all your sites in SharePoint Online."
],
"creationDateTime": "2024-02-29",
"updateDateTime": "2024-02-29",
"products": [
"SharePoint"
],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "2.3.0"
},
{
"key": "POWERSHELL",
"value": "7.4.0"
}
],
"categories": [
"Data",
"Provision",
"Configure"
],
"tags": [
"Connect-PnPOnline",
"Get-PnPConnection",
"Add-PnPNavigationNode",
"Get-PnPNavigationNode",
"Remove-PnPNavigationNode"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-copy-hubsite-navigation/assets/preview.png",
"alt": "Preview of the sample <title>"
}
],
"authors": [
{
"gitHubAccount": "tmaestrini",
"company": "",
"pictureUrl": "https://avatars.githubusercontent.com/u/69770609?v=4",
"name": "Tobias Maestrini"
}
],
"references": [
{
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
}
]
}
]

0 comments on commit d78a828

Please sign in to comment.