Publishing Sites structural navigation bulk export and import of navigation items hide or show


It might be interesting for Site Collection Admins, when there is a need to hide or show navigation items on publishing pages. Using PowerShell it is an easy way, but only the beginning when you want to do it regularly. I worked on an export into a XML file, that is easy readable and we have nice colored tools available on the internet, helping to keep the XML structure when editing. You may make changes in the XML file and use it later for an import.

The UI part for the same:

image

Part 1, the Export:

Put the text into Export-Nav.ps1

################################################################################################

# THIS CODE-SAMPLE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED

# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR

# FITNESS FOR A PARTICULAR PURPOSE.

#

# This sample is not supported under any Microsoft standard support program or service.

# The script is provided AS IS without warranty of any kind. Microsoft further disclaims all

# implied warranties including, without limitation, any implied warranties of merchantability

# or of fitness for a particular purpose. The entire risk arising out of the use or performance

# of the sample and documentation remains with you. In no event shall Microsoft, its authors,

# or anyone else involved in the creation, production, or delivery of the script be liable for

# any damages whatsoever (including, without limitation, damages for loss of business profits,

# business interruption, loss of business information, or other pecuniary loss) arising out of

# the use of or inability to use the sample or documentation, even if Microsoft has been advised

# of the possibility of such damages.

################################################################################################

 

# This script may need to know the URL we want to start and the file to export is needed

# The file should not exists, otherwise the export does not work

Param(

  [Parameter(Mandatory=$True)]

  [string]$SiteColUrl,

  [Parameter(Mandatory=$True)]

  [string]$ExportFile

)

 

Write-Host -ForegroundColor White “This script exports structural navigation information regarding hide or show a navigation item”

Write-Host -ForegroundColor Yellow “From Site Collection : “ $SiteColUrl

Write-Host -ForegroundColor Yellow “Into the file        : “ $ExportFile

 

# Set the Site Collection

$SPSite = Get-SPSite -Identity $SiteColUrl

 

# Construct an out-array to use for data export

$OutArray = @()

 

# Go through each site in the Site Collection

foreach ($SPWeb in $SPSite.AllWebs)

{

if ($SPWeb.IsRootWeb)

{

#Process the root web

 

#Set the Publishing Web

$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($SPWeb)

 

Write-Host -ForegroundColor Gray “Currently working on : “ -NoNewline

$SPPubWeb.Web.Url

 

# Construct an object, that contains URL; Global and Current navigation true or false

$myobj = “” | Select “Url”, “Title”, “WebId”, “Current”,“Global”

# fill the object

$myobj.Url = $SPPubWeb.Url

$myobj.Title = $SPPubWeb.Title

$myobj.WebId = $SPPubWeb.Web.ID

$myobj.Current = $SPPubWeb.IncludeInCurrentNavigation

$myobj.Global = $SPPubWeb.IncludeInGlobalNavigation

 

# Add the object to the out-array

$OutArray += $myobj

 

# Wipe the object just to be sure

$myobj = $null

 

}

 

else

# Now all the SubSites first level

{

## Not a Root-Site but also needs to get the publishing web object

$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($SPWeb)

Write-Host -ForegroundColor Gray “Currently working on : “ -NoNewline

$SPPubWeb.Web.Url

 

# Construct an object, that contains URL; Global and Current navigation true or false

$myobj = “” | Select “Url”, “Title”, “WebId”, “Current”,“Global”

#fill the object

$myobj.Url = $SPPubWeb.Url

$myobj.Title = $SPPubWeb.Title

$myobj.WebId = $SPPubWeb.Web.ID

$myobj.Current = $SPPubWeb.IncludeInCurrentNavigation

$myobj.Global = $SPPubWeb.IncludeInGlobalNavigation

 

# Add the object to the out-array

$OutArray += $myobj

 

# Wipe the object just to be sure

$myobj = $null

 

}

}

 

# Now we can write our data into a File:

# After the loop, export the array as XML

$OutArray | Export-Clixml $ExportFile -NoClobber

 

Write-Host -ForegroundColor Green “We are done, please check the file : “ -NoNewline

$ExportFile

 

You can run from your SharePoint 2013 Management Shell similar to: C:\Users\Administrator.Contoso\Documents\Export-Nav.ps1 –SiteColUrl http://Contoso/Sites/Contoso-Pub –ExportFile “C:\Users\Administrator.Contoso\Documents\Export-Contoso-Pub.xml”

Part 2, the content of the XML file:

<Objs Version=1.1.0.1 xmlns=http://schemas.microsoft.com/powershell/2004/04>

  <Obj RefId=0>

    <TN RefId=0>

      <T>Selected.System.String</T>

      <T>System.Management.Automation.PSCustomObject</T>

      <T>System.Object</T>

    </TN>

    <MS>

      <S N=Url>http://contoso/sites/contoso-pub</S>

      <S N=Title>US-Contoso</S>

      <G N=WebId>04323ef6-d7a3-4e89-8238-262de18a0a49</G>

      <B N=Current>true</B>

      <B N=Global>true</B>

    </MS>

  </Obj>

  <Obj RefId=1>

    <TNRef RefId=0 />

    <MS>

      <S N=Url>http://contoso/sites/contoso-pub/SecSubSite</S>

      <S N=Title>MoreSubsites</S>

      <G N=WebId>facc55ca-577d-4e86-bb24-8429b737d3ef</G>

      <B N=Current>false</B>

      <B N=Global>false</B>

    </MS>

  </Obj>

  <Obj RefId=2>

    <TNRef RefId=0 />

    <MS>

      <S N=Url>http://contoso/sites/contoso-pub/SubSite-First</S>

      <S N=Title>SubSite</S>

      <G N=WebId>65f688f0-11b4-4fa4-97a4-023e2d382252</G>

      <B N=Current>false</B>

      <B N=Global>false</B>

    </MS>

  </Obj>

</Objs>

 

Part 3, edit the XML file:

In this example change false to true. The result should be to have both SubSites visible in the top navigation bar (Global Navigation) and also on the left side (Current Navigation).

Please be careful with editing the file, the code does not contain syntax checks and more, it is just an example for the bulk change of navigation items, hide or show. Some information might not be important because for the import we just need the SiteCol URL, WebId’s, Current and Global. The title should help to identify your SubSite. In any way you can pimp up the script to export much more, like pages or also more levels of SubSites.

Part 4, the Import:

Put the text into Import-Nav.ps1

################################################################################################

# THIS CODE-SAMPLE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED

# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR

# FITNESS FOR A PARTICULAR PURPOSE.

#

# This sample is not supported under any Microsoft standard support program or service.

# The script is provided AS IS without warranty of any kind. Microsoft further disclaims all

# implied warranties including, without limitation, any implied warranties of merchantability

# or of fitness for a particular purpose. The entire risk arising out of the use or performance

# of the sample and documentation remains with you. In no event shall Microsoft, its authors,

# or anyone else involved in the creation, production, or delivery of the script be liable for

# any damages whatsoever (including, without limitation, damages for loss of business profits,

# business interruption, loss of business information, or other pecuniary loss) arising out of

# the use of or inability to use the sample or documentation, even if Microsoft has been advised

# of the possibility of such damages.

################################################################################################

 

# This script may need to know the the filename with all the information, created by Export-Nav.ps1

Param(

  [Parameter(Mandatory=$True)]

  [string]$ImportFile

)

 

Write-Host -ForegroundColor White “This script imports structural navigation settings regarding hide or show a navigation item”

Write-Host -ForegroundColor Yellow “From the file : “ $ImportFile

 

# Import the content from the file into an Array.

$ImportArray = Import-Clixml $ImportFile

 

# How many Items are in the file?

Write-Host -ForegroundColor Gray “We found “ ($ImportArray.Count1) ” SubSites in the file”

 

# Set the Site Collection

# The first entry has to be the Site Collection URL

$SPSite = Get-SPSite -Identity $ImportArray[0].Url

$SPWeb = $SPSite.OpenWeb()

$PuWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($SPWeb)

 

# It makes no sense to do set something on the root, therefore we start with the first SubSite

$i = 1 # Set pointer to 1; 0 is the SiteCol

 

Do ## let it run until the last item has been processed

{

 

    # Take the item out of the array

    $ImportItem = $ImportArray[$i]

      

    Write-Host -ForegroundColor Gray “Currently working on : “ -NoNewline

    $ImportItem.Url

 

    # First let us work on the Current navigation items

    # For the first parameter in Ex/IncludeInNavigation we need to know:

    # https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.navigation.portalnavigation.includeinnavigation.aspx

    # useGlobal means true for global and false for current

 

    if($ImportItem.Current)

    {

        # Current and yes we want to see it

        $PuWeb.Navigation.IncludeInNavigation($false, $ImportItem.WebId)

    }

    else

    {

        # Current and we want to hide the item

        $PuWeb.Navigation.ExcludeFromNavigation($false, $ImportItem.WebId)

       

    }

   

    ## Now let us work on the Global naviation items

    if($ImportItem.Global)

    {

        # Current and yes we want to see it

        $PuWeb.Navigation.IncludeInNavigation($true, $ImportItem.WebId)

    }

    else

    {

        # Current and we want to hide the item

        $PuWeb.Navigation.ExcludeFromNavigation($true, $ImportItem.WebId)

    }

 

 

    $i++

} while ($i -lt $ImportArray.Count)

 

    # Now we need to update it

    $PuWeb.Web.Update()

    $PuWeb.Web.Dispose()

 

    Write-Host -ForegroundColor Green “We are done with “ ($ImportArray.Count1) ” SubSites.”

 

You can run from your SharePoint 2013 Management Shell similar to: C:\Users\Administrator.Contoso\Documents\Import-Nav.ps1 –ImportFile “C:\Users\Administrator.Contoso\Documents\Export-Contoso-Pub.xml”

References during my journey to develop this script:

PublishingPage.IncludeInCurrentNavigation property
https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.publishingpage.includeincurrentnavigation.aspx

PublishingWeb.IncludeInGlobalNavigation property
https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.publishingweb.includeinglobalnavigation.aspx

PortalNavigation.ExcludeFromNavigation method
https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.navigation.portalnavigation.excludefromnavigation.aspx
The faster method instead of using the property when writing.

PortalNavigation.IncludeInNavigation method
https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.publishing.navigation.portalnavigation.includeinnavigation.aspx
The faster method instead of using the property when writing.

A different way and for Pages:
http://stackoverflow.com/questions/228145/how-do-i-programatically-turn-off-show-pages-in-navigation-for-sharepoint

Using the Export-Clixml Cmdlet
https://technet.microsoft.com/en-us/library/ee176824.aspx

Using the Import-Clixml Cmdlet
https://technet.microsoft.com/en-us/library/ee176873.aspx

PowerShell Looping: Understanding and Using Do…While
https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/05/powershell-looping-understanding-and-using-do-while/

about_Comparison_Operators
https://technet.microsoft.com/en-us/library/hh847759.aspx

I tested on a SharePoint 2013 environment, but it may also work on SharePoint 2016 environments.

Comments (0)

Skip to main content