Wednesday, January 13, 2016

PowerShell script to list and export top navigation links

This PowerShell snippet will parse a single site for the top or global navigation links, display in the shell window, and output to an XML file in the following format:

 <Navigation>  
 <Group Title="Regions" Url="http://regions">  
 <Link>  
      <Name>Asia Pacific</Name>  
      <Url>http://apac</Url>  
      <Target />  
 </Link>  
 <Link>  
      <Name>EMEA</Name>  
      <Url>http://emea</Url>  
      <Target />  
 </Link>  
 <Link>  
      <Name>US</Name>  
      <Url>http://us</Url>  
      <Target />  
 </Link>  
 </Group>  
</Navigation>  



 $filename = “d:\PSCommands\topNav.xml”  
 "<Navigation>" | Out-File $filename   
 $webName = "http://sharepoint/"  
 $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.GlobalNavigationNodes   
 $int = 1;  
 foreach ($node in $cNav)  
 {  
 if ($node.TargetParentObjectType -ne "Web" -and $node.TargetParentObjectType -ne "Item") {  
           write-host "$int. $($node.Title) ( $($node.Id) )" -foregroundcolor green  
           "<Group Title=""$($node.Title)"" Url=""$($node.Url)"">" | Out-File $filename -Append  
           $int = $int + 1;  
      if ($node.Children -ne $null)  
      {  
           $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...