Advertisement
private775

SharePoint: Export/Import navigation (PS)

Mar 25th, 2015
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $propNames = @( "Audience", "Target", "Description", "UrlQueryString", "UrlFragment" )
  2.  
  3. <#
  4.  .Synopsis
  5.   Exports site navigation settings to XML file.
  6.  
  7.  .Description
  8.   The function exports the Site navigation - either TOP or CURRENT into the XML file.
  9.   The file may be further used for importing the navigation into other site.
  10.  
  11.  .Parameter WebUrl
  12.   The Url of the site for the navigation export.
  13.  
  14.  .Parameter Path
  15.   The path to the export file.
  16.  
  17.  .Parameter NavigationType
  18.   The type of the navigation to export:
  19.   TopNavigationBar - top navigation
  20.   QuickLaunch - quick launch (right side navigation)
  21.   Default: TopNavigationBar
  22.  
  23.  .Example
  24.    # Export Top Navigation.
  25.    Export-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml
  26.  
  27.  .Example
  28.    # Export Quick Launch Navigation.
  29.    Export-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml -NavigationType QuickLaunch
  30. #>
  31. function Export-SPNavigation {
  32.     [CmdletBinding()]
  33.     Param(
  34.         [parameter(Mandatory=$true, Position=0)]
  35.         [String]
  36.         $WebUrl,
  37.  
  38.         [parameter(Mandatory=$true, Position=1)]
  39.         [String]
  40.         $Path,
  41.        
  42.         [parameter(Position=2)]
  43.         [ValidateSet("TopNavigationBar", "QuickLaunch")]
  44.         [String]
  45.         $NavigationType = "TopNavigationBar"
  46.     )
  47.  
  48.     $outDir = [System.IO.Path]::GetDirectoryName($Path)
  49.     [xml]$xml = "<Navigation />"
  50.  
  51.     $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
  52.     if($w -eq $null){
  53.         throw "Cannot open site: $($WebUrl)"
  54.     }
  55.    
  56.     $navExcludes = ""
  57.     if($w.AllProperties.ContainsKey("__GlobalNavigationExcludes")){
  58.             $navExcludes = $w.AllProperties["__GlobalNavigationExcludes"]
  59.     }
  60.    
  61.     $navigationNodeCollection = $w.Navigation.TopNavigationBar
  62.  
  63.     if($NavigationType -eq "QuickLaunch"){
  64.         $navigationNodeCollection = $w.Navigation.QuickLaunch
  65.         $navExcludes = ""
  66.         if($w.AllProperties.ContainsKey("__CurrentNavigationExcludes")){
  67.             $navExcludes = $w.AllProperties["__CurrentNavigationExcludes"]
  68.         }
  69.     }
  70.    
  71.     $hidePages = $false
  72.     $hideSubSites = $false
  73.     if([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($w)){
  74.         $pw = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($w)
  75.         $hidePages = -not $pw.Navigation.GlobalIncludePages
  76.         $hideSubSites = -not $pw.Navigation.GlobalIncludeSubSites
  77.         if($NavigationType -eq "QuickLaunch"){
  78.             $hidePages = -not $pw.Navigation.CurrentIncludePages
  79.             $hideSubSites = -not $pw.Navigation.CurrentIncludeSubSites
  80.         }
  81.     }
  82.    
  83.     if($navigationNodeCollection.Count -gt 0){
  84.         foreach($node in $navigationNodeCollection){
  85.             addNavNode $xml $xml.DocumentElement $node $navExcludes $hidePages $hideSubSites
  86.         }
  87.    
  88.         If (-not (Test-Path $outDir)){
  89.             md $outDir
  90.         }
  91.         if(Test-Path $Path){
  92.             Remove-Item -Confirm:$false -Force $Path
  93.         }  
  94.         $xml.Save($Path)
  95.     }
  96. }
  97.  
  98. function addNavNode($xml, $xmlParentNode, $navNode, $navExcludes, $hidePages, $hideSubSites){
  99.     $export = $true
  100.     $nodeId = getNodeId $navNode
  101.  
  102.     if($nodeId -ne $null -and $navExcludes -ne $null -and $navExcludes.Contains($nodeId)){
  103.         $export = $false
  104.     }
  105.  
  106.     $isHeading = $navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Heading"
  107.     if($isHeading){
  108.         $export = $true
  109.     }
  110.  
  111.     if($hidePages){
  112.         if($navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Page"){
  113.             $export = $false
  114.         }
  115.     }
  116.    
  117.     if($hideSubSites){
  118.         if($navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Area"){
  119.             $export = $false
  120.         }
  121.     }
  122.  
  123.     if($export){
  124.         $xmlNode = $xml.CreateElement("NavNode");
  125.         $url = $xml.CreateAttribute("Url")
  126.         $url.Value = $navNode.Url
  127.         $url = $xmlNode.Attributes.Append($url)
  128.         $title = $xml.CreateAttribute("Title")
  129.         $title.Value = $navNode.Title
  130.         $title = $xmlNode.Attributes.Append($title)
  131.         foreach($propName in $propNames){
  132.             if($navNode.Properties.ContainsKey($propName) -and $navNode.Properties[$propName] -ne $null  -and $navNode.Properties[$propName] -ne ''){
  133.                 $prop = $xml.CreateAttribute($propName)
  134.                 $prop.Value = $navNode.Properties[$propName]
  135.                 $prop = $xmlNode.Attributes.Append($prop)
  136.             }
  137.         }
  138.         if($navNode.Children.Count -gt 0){
  139.             $xmlChldNode = $xml.CreateElement("Children");
  140.             foreach($chldNode in $navNode.Children){
  141.                 addNavNode $xml $xmlChldNode $chldNode $navExcludes $hidePages $hideSubSites
  142.             }
  143.             [void]($xmlNode.AppendChild($xmlChldNode))
  144.         }
  145.         $xmlNode = $xmlParentNode.AppendChild($xmlNode)
  146.     }
  147. }
  148.  
  149. <#
  150.  .Synopsis
  151.   Imports site navigation settings from XML file.
  152.  
  153.  .Description
  154.   The function imports the Site navigation - either TOP or CURRENT from the XML file.
  155.  
  156.  .Parameter WebUrl
  157.   The Url of the site for the navigation import.
  158.  
  159.  .Parameter Path
  160.   The path to the import file.
  161.  
  162.  .Parameter NavigationType
  163.   The type of the navigation to import:
  164.   TopNavigationBar - top navigation
  165.   QuickLaunch - quick launch (right side navigation)
  166.   Default: TopNavigationBar
  167.  
  168.  .Example
  169.    # Import Top Navigation.
  170.    Import-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml
  171.  
  172.  .Example
  173.    # Import Quick Launch Navigation.
  174.    Import-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml -NavigationType QuickLaunch
  175. #>
  176. function Import-SPNavigation {
  177.     [CmdletBinding()]
  178.     Param(
  179.         [parameter(Mandatory=$true, Position=0)]
  180.         [alias("Url","WebUrl")]
  181.         [String]
  182.         $WebUrl,
  183.  
  184.         [parameter(Mandatory=$true, Position=1)]
  185.         [alias("File")]
  186.         [String]
  187.         $Path,
  188.        
  189.         [parameter(Position=2)]
  190.         [ValidateSet("TopNavigationBar", "QuickLaunch")]
  191.         [String]
  192.         $NavigationType = "TopNavigationBar"
  193.     )
  194.  
  195.     if(-not (Test-Path $Path)){
  196.         Throw "The import file doesn't exist: $($Path)"
  197.     }
  198.  
  199.     $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
  200.     if($w -eq $null){
  201.         throw "Cannot open site: $($WebUrl)"
  202.     }
  203.    
  204.     $navigationNodeCollection = $w.Navigation.TopNavigationBar
  205.     if($NavigationType -eq "QuickLaunch"){
  206.         $navigationNodeCollection = $w.Navigation.QuickLaunch
  207.     }
  208.    
  209.     $len = $navigationNodeCollection.Count
  210.     if($len -gt 0){ foreach($i in $($len - 1)..0){$navigationNodeCollection[$i].Delete()}}
  211.  
  212.     $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
  213.     $navigationNodeCollection = $w.Navigation.TopNavigationBar
  214.     if($NavigationType -eq "QuickLaunch"){
  215.         $navigationNodeCollection = $w.Navigation.QuickLaunch
  216.     }
  217.  
  218.     $xml = [xml](Get-Content $Path)
  219.     importNavigation $navigationNodeCollection $xml.Navigation
  220. }
  221.  
  222. function importNavigation($parent, $nodes){
  223.     $len = $nodes.NavNode.Length
  224.     if($len -gt 0){
  225.         for($i=0; $i -lt $len; $i++){
  226.             $node = $nodes.NavNode[$i]
  227.             $navNode = new-object -TypeName Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList $node.Title,$node.Url,$true
  228.             $parent.AddAsLast($navNode)
  229.             "Properties: $($navNode.Properties -eq $null)"
  230.             foreach($propName in $propNames){
  231.                 if($node.$($propName) -ne $null){
  232.                     $navNode.Properties.Add($propName, $node.$($propName))
  233.                     $navNode.Update()
  234.                 }
  235.             }
  236.             if($node.Children -ne $null -and $node.Children.NavNode.Length -gt 0){
  237.                 importNavigation $navNode.Children $node.Children
  238.             }
  239.         }
  240.     }
  241. }
  242.  
  243. function getNodeId($node){
  244.     $w = $node.Navigation.Web
  245.     $ret = $null
  246.     if($node.TargetParentObjectType -eq "Web"){
  247.         $ww = $w.Site.OpenWeb($node.Url)
  248.         $ret = $ww.ID.ToString('d').ToLower()
  249.         $w.Dispose()
  250.     } elseif ($node.TargetParentObjectType -eq "Item"){
  251.         $ii = $w.GetListItem($node.Url)
  252.         $ret = $ii.UniqueId.ToString('d').ToLower()
  253.     }
  254.     return $ret
  255. }
  256.  
  257. export-modulemember *-*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement