Home Get All hub site and its main sites navigation nodes and update the navigation nodes if needed
Post
Cancel

Get All hub site and its main sites navigation nodes and update the navigation nodes if needed

Summary

This sample demonstrates how to get all hub site and its main sites navigation nodes and update the navigation nodes if needed

Pre-requisites

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

# Connect to the SharePoint Admin site
$adminUrl = "https://[tenant]-admin.sharepoint.com"

Connect-PnPOnline -Url $adminUrl -UseWebLogin

# Get all main sites in the hub
$hubSiteUrl = "https://[tenant].sharepoint.com/sites/[hubsite]"
$mainSites = Get-PnPHubSiteChild -Identity $hubSiteUrl

# Initialize an array to hold the results
$myResults = @()

foreach ($site in $mainSites) {
    # Switch context to the main site
    Connect-PnPOnline -Url $site -UseWebLogin

    # Get top-level navigation nodes
    $navNodes = Get-PnPNavigationNode # -Location TopNavigationBar

    foreach ($node in $navNodes) {
        # Add parent node to results
        $myResults += [PSCustomObject]@{
            NodeCode = $node.Id
            NodeTitle = $node.Title
            NodeType = "Parent"
            URL      = $node.Url
        }

        # Get child nodes
        $childNodes = Get-PnPNavigationNode -Id $node.Id

        foreach ($childNode in $childNodes.Children) {

            # Check if the child node title is "Confluence" and change it to "Confluence2"
            if ($childNode.Title -eq "Confluence") {
                # Rename the child node to "Confluence2"
                  $childNode.Title = "Confluence2"
                  $childNode.Url = "/Lists/TestLlist"
                  $childNode.Update()
                  $childNode.Context.ExecuteQuery()
               
                $childNode.Title = "Confluence2" # Update local object for display
            }

            # Add child nodes to results
            $myResults += [PSCustomObject]@{
                NodeCode = $childNode.Id
                NodeTitle = $childNode.Title
                NodeType = "Child"
                URL      = $childNode.Url
            }
        }
    }

    Disconnect-PnPOnline
}

# Display the results in a formatted table
$myResults | Format-Table -AutoSize


PNP Script sample site

Script sample site

This post is licensed under CC BY 4.0 by the author.

SharingIsCaring. Advent Calendar - A Fun Christmas-Themed SPFx

Tip of the day change quicklink navigation orientation