Wednesday, September 30, 2015

PowerShell list SharePoint Global Navigation headers and subheader links

This script will traverse the top navigation bar links in a SharePoint site and retrieve all the main headers plus dropdown links, and optionally list the link URLs.

Tried on SharePoint publishing sites.
How do you know if publishing is turned on?
If you see the Navigation link instead of Top Link Bar / Quick Launch, the feature is turned on.



 $filename = “d:\powershell\topNav.xml”  
 "<Navigation>" | Out-File $filename   
 $webName = "http://dev-portal"  
 $RemoveSnapInWhenDone = $False  
 if (-not (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue))  
 {  
   Add-PSSnapin Microsoft.SharePoint.PowerShell  
   $RemoveSnapInWhenDone = $True  
 }  
 $web = Get-SPWeb $webName  
 $pub = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)  
 #$cNav = $pub.Navigation.CurrentNavigationNodes   
 $cNav = $pub.Navigation.GlobalNavigationNodes   
 #$cNav = $web.Navigation.TopNavigationBar  
 #$cNav = $web.Navigation.GlobalNodes   
 $int = 1;  
 foreach ($node in $cNav)  
 {  
      if ($node.Children -ne $null)  
      {  
           write-host "$int. $($node.Title) ( $($node.Id) )" -foregroundcolor green  
           "<Group Title=""$($node.Title)"">" | Out-File $filename -Append  
           $int = $int + 1;  
           $subInt = 1;  
           foreach ($childNode in $node.Children)  
          {  
               write-host "$int.$subInt $($childNode.Title) ( $($childNode.Url) )"  
                "<Link>" | Out-File $filename -Append  
                "     <Name>$($childNode.Title)</Name>" | Out-File $filename -Append  
                "     <Url>$($childNode.Url)</Url>" | Out-File $filename -Append  
                "     <Target />" | Out-File $filename -Append  
                "</Link>" | Out-File $filename      -Append            
                $subInt = $subInt + 1;  
          }  
           "</Group>" | Out-File $filename -Append  
      }  
 }  
 "</Navigation>" | Out-File $filename -Append  
 $web.Dispose()  

No comments:

Related Posts Plugin for WordPress, Blogger...