Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $propNames = @( "Audience", "Target", "Description", "UrlQueryString", "UrlFragment" )
- <#
- .Synopsis
- Exports site navigation settings to XML file.
- .Description
- The function exports the Site navigation - either TOP or CURRENT into the XML file.
- The file may be further used for importing the navigation into other site.
- .Parameter WebUrl
- The Url of the site for the navigation export.
- .Parameter Path
- The path to the export file.
- .Parameter NavigationType
- The type of the navigation to export:
- TopNavigationBar - top navigation
- QuickLaunch - quick launch (right side navigation)
- Default: TopNavigationBar
- .Example
- # Export Top Navigation.
- Export-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml
- .Example
- # Export Quick Launch Navigation.
- Export-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml -NavigationType QuickLaunch
- #>
- function Export-SPNavigation {
- [CmdletBinding()]
- Param(
- [parameter(Mandatory=$true, Position=0)]
- [String]
- $WebUrl,
- [parameter(Mandatory=$true, Position=1)]
- [String]
- $Path,
- [parameter(Position=2)]
- [ValidateSet("TopNavigationBar", "QuickLaunch")]
- [String]
- $NavigationType = "TopNavigationBar"
- )
- $outDir = [System.IO.Path]::GetDirectoryName($Path)
- [xml]$xml = "<Navigation />"
- $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
- if($w -eq $null){
- throw "Cannot open site: $($WebUrl)"
- }
- $navExcludes = ""
- if($w.AllProperties.ContainsKey("__GlobalNavigationExcludes")){
- $navExcludes = $w.AllProperties["__GlobalNavigationExcludes"]
- }
- $navigationNodeCollection = $w.Navigation.TopNavigationBar
- if($NavigationType -eq "QuickLaunch"){
- $navigationNodeCollection = $w.Navigation.QuickLaunch
- $navExcludes = ""
- if($w.AllProperties.ContainsKey("__CurrentNavigationExcludes")){
- $navExcludes = $w.AllProperties["__CurrentNavigationExcludes"]
- }
- }
- $hidePages = $false
- $hideSubSites = $false
- if([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($w)){
- $pw = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($w)
- $hidePages = -not $pw.Navigation.GlobalIncludePages
- $hideSubSites = -not $pw.Navigation.GlobalIncludeSubSites
- if($NavigationType -eq "QuickLaunch"){
- $hidePages = -not $pw.Navigation.CurrentIncludePages
- $hideSubSites = -not $pw.Navigation.CurrentIncludeSubSites
- }
- }
- if($navigationNodeCollection.Count -gt 0){
- foreach($node in $navigationNodeCollection){
- addNavNode $xml $xml.DocumentElement $node $navExcludes $hidePages $hideSubSites
- }
- If (-not (Test-Path $outDir)){
- md $outDir
- }
- if(Test-Path $Path){
- Remove-Item -Confirm:$false -Force $Path
- }
- $xml.Save($Path)
- }
- }
- function addNavNode($xml, $xmlParentNode, $navNode, $navExcludes, $hidePages, $hideSubSites){
- $export = $true
- $nodeId = getNodeId $navNode
- if($nodeId -ne $null -and $navExcludes -ne $null -and $navExcludes.Contains($nodeId)){
- $export = $false
- }
- $isHeading = $navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Heading"
- if($isHeading){
- $export = $true
- }
- if($hidePages){
- if($navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Page"){
- $export = $false
- }
- }
- if($hideSubSites){
- if($navNode.Properties.ContainsKey("NodeType") -and $navNode.Properties["NodeType"] -eq "Area"){
- $export = $false
- }
- }
- if($export){
- $xmlNode = $xml.CreateElement("NavNode");
- $url = $xml.CreateAttribute("Url")
- $url.Value = $navNode.Url
- $url = $xmlNode.Attributes.Append($url)
- $title = $xml.CreateAttribute("Title")
- $title.Value = $navNode.Title
- $title = $xmlNode.Attributes.Append($title)
- foreach($propName in $propNames){
- if($navNode.Properties.ContainsKey($propName) -and $navNode.Properties[$propName] -ne $null -and $navNode.Properties[$propName] -ne ''){
- $prop = $xml.CreateAttribute($propName)
- $prop.Value = $navNode.Properties[$propName]
- $prop = $xmlNode.Attributes.Append($prop)
- }
- }
- if($navNode.Children.Count -gt 0){
- $xmlChldNode = $xml.CreateElement("Children");
- foreach($chldNode in $navNode.Children){
- addNavNode $xml $xmlChldNode $chldNode $navExcludes $hidePages $hideSubSites
- }
- [void]($xmlNode.AppendChild($xmlChldNode))
- }
- $xmlNode = $xmlParentNode.AppendChild($xmlNode)
- }
- }
- <#
- .Synopsis
- Imports site navigation settings from XML file.
- .Description
- The function imports the Site navigation - either TOP or CURRENT from the XML file.
- .Parameter WebUrl
- The Url of the site for the navigation import.
- .Parameter Path
- The path to the import file.
- .Parameter NavigationType
- The type of the navigation to import:
- TopNavigationBar - top navigation
- QuickLaunch - quick launch (right side navigation)
- Default: TopNavigationBar
- .Example
- # Import Top Navigation.
- Import-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml
- .Example
- # Import Quick Launch Navigation.
- Import-SPNavigation -WebUrl http://bhbconnect -Path c:\temp\topNav.xml -NavigationType QuickLaunch
- #>
- function Import-SPNavigation {
- [CmdletBinding()]
- Param(
- [parameter(Mandatory=$true, Position=0)]
- [alias("Url","WebUrl")]
- [String]
- $WebUrl,
- [parameter(Mandatory=$true, Position=1)]
- [alias("File")]
- [String]
- $Path,
- [parameter(Position=2)]
- [ValidateSet("TopNavigationBar", "QuickLaunch")]
- [String]
- $NavigationType = "TopNavigationBar"
- )
- if(-not (Test-Path $Path)){
- Throw "The import file doesn't exist: $($Path)"
- }
- $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
- if($w -eq $null){
- throw "Cannot open site: $($WebUrl)"
- }
- $navigationNodeCollection = $w.Navigation.TopNavigationBar
- if($NavigationType -eq "QuickLaunch"){
- $navigationNodeCollection = $w.Navigation.QuickLaunch
- }
- $len = $navigationNodeCollection.Count
- if($len -gt 0){ foreach($i in $($len - 1)..0){$navigationNodeCollection[$i].Delete()}}
- $w = Get-SPWeb $WebUrl -ErrorAction SilentlyContinue
- $navigationNodeCollection = $w.Navigation.TopNavigationBar
- if($NavigationType -eq "QuickLaunch"){
- $navigationNodeCollection = $w.Navigation.QuickLaunch
- }
- $xml = [xml](Get-Content $Path)
- importNavigation $navigationNodeCollection $xml.Navigation
- }
- function importNavigation($parent, $nodes){
- $len = $nodes.NavNode.Length
- if($len -gt 0){
- for($i=0; $i -lt $len; $i++){
- $node = $nodes.NavNode[$i]
- $navNode = new-object -TypeName Microsoft.SharePoint.Navigation.SPNavigationNode -ArgumentList $node.Title,$node.Url,$true
- $parent.AddAsLast($navNode)
- "Properties: $($navNode.Properties -eq $null)"
- foreach($propName in $propNames){
- if($node.$($propName) -ne $null){
- $navNode.Properties.Add($propName, $node.$($propName))
- $navNode.Update()
- }
- }
- if($node.Children -ne $null -and $node.Children.NavNode.Length -gt 0){
- importNavigation $navNode.Children $node.Children
- }
- }
- }
- }
- function getNodeId($node){
- $w = $node.Navigation.Web
- $ret = $null
- if($node.TargetParentObjectType -eq "Web"){
- $ww = $w.Site.OpenWeb($node.Url)
- $ret = $ww.ID.ToString('d').ToLower()
- $w.Dispose()
- } elseif ($node.TargetParentObjectType -eq "Item"){
- $ii = $w.GetListItem($node.Url)
- $ret = $ii.UniqueId.ToString('d').ToLower()
- }
- return $ret
- }
- export-modulemember *-*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement