Welcome to the navigation

Dolor excepteur officia dolor do labore fugiat in id dolore duis culpa ut anim laborum, laboris elit, ut et sunt est magna pariatur, aliquip reprehenderit. Ut culpa incididunt consequat, mollit minim magna velit nostrud lorem ut adipisicing occaecat ea sint eu laborum, aliquip non dolore exercitation consectetur dolor dolor enim

Yeah, this will be replaced... But please enjoy the search!

Restoring a backed up site collection with managed navigation in another web application

​​The scenario may not be the most common from an administrators view but from a developers view this is very common since we often want to have test and development environments that are configured and kitted in the same way as the production environment is.

The goal of this post is not to provide a complete solution to all scenarios that may exist when doing this but to give you an idea of what is possible.

Download the Backup and restore scripts (or copy/paste from below)

The simple scenario

If things were simple we would always do this, and it may sometimes suffice. All you have to do is run a Backup-SPSite and a Restore-SPSite.

 

Reality

When involving Managed Navigation things aren't that simple, especially when you are working with managed navigation and are shifting web applications.

The process usually looks more like this

 

What differ is

  • Two exports
  • The import of the navigation term set which includes some logic.

Backing up site collection and termset

The script offers a few parameters to setup

$site = "http://sp2013dev"
$localPath = "c:\temp\"
$remotePath = "" #set if you want to move the backups when done
$backupName = "sp2013"
$serviceName = "Managed Metadata Service"

 

Restoring an exported site collection and the termset

Setting up

$site = "http://sp2013dev/" # The site you are restoring to
$originatingSite = "http://www.herlitz.nu" # Set this if you are restoring from another webapplication, remove it if you are restoring to the same web app
$path = "c:\temp\"
$siteBackupName = "www_herlitz_nu_20141101.bak"
$termBackupName = "www_herlitz_nu_terms_20141101.bak"
$serviceName = "Managed Metadata Service" 

 

If everything went well you should be able to use the same content in your restored environment

Download the Backup and restore scripts (or copy/paste from below)

Scripts

Backup

##################################
# Backup script
# By Eric Herlitz, www.herlitz.nu
##################################
$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
    Write-Host "[INIT] Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
 
##############################
# Set default values
##############################
$site = "http://sp2013dev"
$localPath = "c:\temp\"
$remotePath = "" #set if you want to move the backups when done
$backupName = "sp2013"
$serviceName = "Managed Metadata Service"
 
##############################
# Default paths
##############################
$uuid = (Get-Date -format yyyyMMdd)
$sitePath = [System.String]::Concat($localPath.TrimEnd("\"), "\", $backupName, "_", $uuid, ".bak")
$termPath = [System.String]::Concat($localPath.TrimEnd("\"), "\", $backupName, "_terms_", $uuid, ".bak")
 
##############################
# Backup the site
##############################
Try {
    Write-Host "Backing up the site" -ForegroundColor DarkYellow
    Backup-SPSite $site -Path $sitePath -Force -ErrorAction Stop
    Write-Host "Successfully backed up the Site [$sitePath]" -ForegroundColor Green
} Catch {
    Write-Host "Could not backup the site ["$_.Exception.Message"]" -ForegroundColor Red
    Exit
}
 
##############################
# Backup managed navigation
##############################
Write-Host "Backing up managed navigation" -ForegroundColor DarkYellow
 
# Get the MetadataServiceApplication
Try {
    Write-Host "> Fetching the MetadataServiceApplication" -ForegroundColor DarkYellow
    $metadataApplication = Get-SPMetadataServiceApplication -Identity $serviceName -ErrorAction Stop
} Catch {
    Write-Host $_.Exception.Message -ForegroundColor Red
    Exit
}
 
# ...and the default MetadataWebServiceApplicationProxy
if($PSVersionTable.PSVersion.Major > 3) {
    $defaultSiteCollectionTaxonomy = $metadataApplication.ServiceApplicationProxyGroup.Proxies.Where({$_.Properties.ContainsKey("IsDefaultSiteCollectionTaxonomy")})
} else {
    $defaultSiteCollectionTaxonomy = $metadataApplication.ServiceApplicationProxyGroup.Proxies | Where-Object { $_.Properties.ContainsKey("IsDefaultSiteCollectionTaxonomy") }
}
 
 
if($defaultSiteCollectionTaxonomy.Count -and $defaultSiteCollectionTaxonomy[0].GetType().Name -eq "MetadataWebServiceApplicationProxy") {
 
    Try {
        Write-Host "> Exporting terms" -ForegroundColor DarkYellow
        Export-SPMetadataWebServicePartitionData $metadataApplication.Id -ServiceProxy $defaultSiteCollectionTaxonomy[0].Name -Path $termPath -ErrorAction Stop
        Write-Host "Successfully exported the terms to $termPath" -ForegroundColor Green
    } Catch {
        Write-Host "Could not export the terms ["$_.Exception.Message"]" -ForegroundColor Red
        Exit
    }
} else {
    Write-Host "Could not fetch the MetadataWebServiceApplicationProxy, please reconfigure the script. EXITING" -ForegroundColor Red
    Exit
}
 
##############################
# Move the export to another location
##############################
if($remotePath.Length) {
    Write-Host "Moving the files" -ForegroundColor DarkYellow
    Try {
        $copySitePath = [System.String]::Concat($remotePath.TrimEnd("\"), "\", $backupName, "_", $uuid, ".bak")
        $copyTermPath = [System.String]::Concat($remotePath.TrimEnd("\"), "\", $backupName, "_terms_", $uuid, ".bak")
 
        Move-Item $sitePath $copySitePath -Force -ErrorAction Stop
        Write-Host "Successfully moved [$sitePath] to [$copySitePath]" -ForegroundColor Green
 
        Move-Item $termPath $copyTermPath -Force -ErrorAction Stop
        Write-Host "Successfully moved [$termPath] to [$copyTermPath]" -ForegroundColor Green
    } Catch {
        Write-Host "Could not move the backup [$_.Exception.Message]" -ForegroundColor Red
        Exit
    }
}
 
Write-Host "The export seems done!" -ForegroundColor Green

Restore

##################################
# Restore script
# By Eric Herlitz, www.herlitz.nu
##################################
$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
    Write-Host "[INIT] Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
 
##############################
# Set default values
##############################
$site = "http://sp2013dev/"
$originatingSite = "http://www.herlitz.nu" # Set this if you are restoring from another webapplication
$path = "c:\temp\"
$siteBackupName = "www_herlitz_nu_20141101.bak"
$termBackupName = "www_herlitz_nu_terms_20141101.bak"
$serviceName = "Managed Metadata Service"
 
##############################
# Default paths
##############################
$backupPath = [System.String]::Concat($path.TrimEnd("\"), "\", $siteBackupName)
$termPath = [System.String]::Concat($path.TrimEnd("\"), "\", $termBackupName)
 
 
##############################
# Restore the site
##############################
Try {
    Write-Host "Restoring site" -ForegroundColor DarkYellow
 
    if(Test-Path $site){
        Restore-SPSite $site -Path $backupPath -Force -ErrorAction Stop
    } else {
        Restore-SPSite $site -Path $backupPath -ErrorAction Stop
    }
 
    Write-Host "Successfully restored the site [$site]" -ForegroundColor Green
} Catch {
    Write-Host "Could not backup the site ["$_.Exception.Message"]" -ForegroundColor Red
    Exit
}
 
 
##############################
# Restore managed navigation
##############################
Write-Host "Restoring managed navigation" -ForegroundColor DarkYellow
 
# Get the MetadataServiceApplication
Try {
    Write-Host "> Fetching the MetadataServiceApplication" -ForegroundColor DarkYellow
    $metadataApplication = Get-SPMetadataServiceApplication -Identity $serviceName -ErrorAction Stop
} Catch {
    Write-Host $_.Exception.Message -ForegroundColor Red
    Exit
}
 
 
# ...and the default MetadataWebServiceApplicationProxy
if($PSVersionTable.PSVersion.Major > 3) {
    $defaultSiteCollectionTaxonomy = $metadataApplication.ServiceApplicationProxyGroup.Proxies.Where({$_.Properties.ContainsKey("IsDefaultSiteCollectionTaxonomy")})
} else {
    $defaultSiteCollectionTaxonomy = $metadataApplication.ServiceApplicationProxyGroup.Proxies | Where-Object { $_.Properties.ContainsKey("IsDefaultSiteCollectionTaxonomy") }
}
 
if($defaultSiteCollectionTaxonomy.Count -and $defaultSiteCollectionTaxonomy[0].GetType().Name -eq "MetadataWebServiceApplicationProxy") {
 
    Try {
        Write-Host "> Importing terms" -ForegroundColor DarkYellow
        Import-SPMetadataWebServicePartitionData $metadataApplication.Id -ServiceProxy $defaultSiteCollectionTaxonomy[0].Name -Path $termPath -OverwriteExisting -ErrorAction Stop
        Write-Host "Successfully imported the terms" -ForegroundColor Green
    } Catch {
        Write-Host "Could not import the terms ["$_.Exception.Message"]" -ForegroundColor Red
        if($_.Exception.Message.Contains("bulk")) {
            Write-Host "You must give the $serviceName account ( probably " $metadataApplication.ApplicationPool.ProcessAccount.Name ") the SQL server BULK ADMIN role" -ForegroundColor Yellow
        }
        Exit
    }
} else {
    Write-Host "Could not fetch the MetadataWebServiceApplicationProxy, please reconfigure the script. EXITING" -ForegroundColor Red
    Exit
}
 
##############################
# Attach to site collection
##############################
Write-Host "Attaching the imported terms to the navigation" -ForegroundColor DarkYellow
 
Try {
    Write-Host "> Fetching sites, webs, termstores and termgroups" -ForegroundColor DarkYellow
 
    $spSite = Get-SPSite $site
    $spWeb = $spSite.RootWeb
    $taxSession = Get-SPTaxonomySession -Site $spSite
 
    $termStore = $taxSession.TermStores[$serviceName]
    $termGroup = $termStore.GetSiteCollectionGroup($spSite)
 
    Write-Host "> Fetching the navigation termset" -ForegroundColor DarkYellow
    $navigationTermset = $termStore.GetTermSetsWithCustomProperty("_Sys_Nav_IsNavigationTermSet") | Where-Object {$_.CustomProperties.ContainsKey("_Sys_Nav_AttachedWeb_OriginalUrl") -and $_.CustomProperties["_Sys_Nav_AttachedWeb_OriginalUrl"] -eq $originatingSite}
 
    if($navigationTermset) {
        Write-Host "Initiating the attach process ot the term set" -ForegroundColor Green
 
        Write-Host "> Moving the imported termset to the site group" -ForegroundColor DarkYellow
        $termGroup = $termStore.GetSiteCollectionGroup($spSite)
        $navigationTermset.Move($termGroup)
        $termStore.CommitAll()
 
        #chage to structural nav, we do this to push sharepoint a bit
        Write-Host "> Juggling with the navigation" -ForegroundColor DarkYellow
        $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
 
        $webNavSettings = [Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings]($spWeb)
        $webNavSettingsGlobal = $webNavSettings.GlobalNavigation.Source
        $webNavSettingsCurrent = $webNavSettings.CurrentNavigation.Source
 
        $webNavSettings.GlobalNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::PortalProvider
        $webNavSettings.CurrentNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::PortalProvider
        $webNavSettings.Update()
 
        #reset to managed nav
        $webNavSettings.GlobalNavigation.Source = $webNavSettingsGlobal
        $webNavSettings.GlobalNavigation.TermSetId = $navigationTermset.Id
        $webNavSettings.GlobalNavigation.TermStoreId = $termStore.Id
        $webNavSettings.AddNewPagesToNavigation = $true
        $webNavSettings.CreateFriendlyUrlsForNewPages = $true
 
        $webNavSettings.CurrentNavigation.Source = $webNavSettingsCurrent
 
        $webNavSettings.Update()
 
        Write-Host "The import seems done!" -ForegroundColor Green
        Write-Host "Please wait for the iisreset to finish, it may also take a minuter or two until the navigation is rebuilt!" -ForegroundColor Green
 
    } else {
        Write-Host "Could not initiate the attach process ot the term set, please ensure the originatingSite parameter is correct. Available termsets are" -ForegroundColor Red
 
        foreach($termSetItem in $termStore.GetTermSetsWithCustomProperty("_Sys_Nav_IsNavigationTermSet") | Where-Object {$_.CustomProperties.ContainsKey("_Sys_Nav_AttachedWeb_OriginalUrl")}) {
            Write-Host "> "$termSetItem.CustomProperties["_Sys_Nav_AttachedWeb_OriginalUrl"] -ForegroundColor Yellow
        }
    }
} Catch {
    Write-Host $_.Exception.Message -ForegroundColor Red
    Exit
} Finally {
    iisreset
}
Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author