PowerShell – Copy Current Navigation from SharePoint Site to another SharePoint Site

Very Handy! Use this in a content migration scenario, or updating many sites with the same navigation.

$sourcewebURL = "http://teams/sites/teamsiteA"
$targetwebURL = "http://teams/sites/teamsiteB"
$find = "/teamsiteA/"
$replace = "/teamsiteB/"
$targetweb = get-spweb $targetwebURL
$sourceweb = get-spweb $sourcewebURL
$targetpub = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($targetweb)
$targetnav = $targetpub.navigation.currentnavigationnodes
$currentIds = @();
$targetpub.Navigation.currentnavigationnodes | ForEach-Object { $currentIds = $currentIds + $_.Id; }
$currentIds | ForEach-Object {
$currentNode = $targetweb.navigation.GetNodeById($_);
$targetnav.Delete($currentNode)}
$sourcepub = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($sourceweb)
$sourcenavs = $sourcepub.navigation.currentnavigationnodes
foreach($sourcenav in $sourcenavs)
{
$targetnavurl = $sourcenav.URL.replace($find,$replace)
$targetnavtitle = $sourcenav.Title.replace($find,$replace)
$newnavmethod = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode
$heading = $newnavmethod.Invoke($targetnavtitle, $targetnavurl, [Microsoft.SharePoint.Publishing.NodeTypes]::Heading, $targetnav);
$sourcenavlinks = $sourcenav.children
$headingchildren = $heading.children;
foreach($sourcenavlink in $sourcenavlinks)
{
$targetnavlinkurl = $sourcenavlink.URL.replace($find,$replace)
$targetnavlinktitle = $sourcenavlink.Title.replace($find,$replace)
$targetnavlink = $newnavmethod.Invoke($targetnavlinktitle,$targetnavlinkurl, [Microsoft.SharePoint.Publishing.NodeTypes]::AuthoredLinkPlain, $headingchildren)
}
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to PowerShell – Copy Current Navigation from SharePoint Site to another SharePoint Site

  1. f1raPete says:

    I like it! Very useful! 🙂

    Unfortunately, it doesn’t copy over the Audience Targeting configuration, but at least half the job is done! 🙂

  2. f1ra says:

    It seems that the Audience for each nav link is available at :

    $sourcenav.properties[‘Audience’]

    Trying to figure out how to include it in this script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s