Wednesday, March 18, 2015

Powershell update all navigation URL to be relative instead of absolete

I have used this powershell script to update all navigation items in a site collection to use only relative URLs instead of absolute ones.

function ProcessWeb($currentWeb)
   Write-Host -ForegroundColor Green "Processing web" $currentWeb.Url
   $currentWeb.AllowUnsafeUpdates = 1
   UpdateNavigation($currentWeb)
   $currentWeb.Update()
   $currentWeb.AllowUnsafeUpdates = 0
   foreach($sub in $currentWeb.Webs)
   {
         ProcessWeb($sub)
   }       
}

function UpdateNavigation($web)
{
   
    $topNavigationNodes = $web.Navigation.TopNavigationBar
    UpdateNavNodes($topNavigationNodes);
    $quickLaunchNodes = $web.Navigation.QuickLaunch
    UpdateNavNodes($quickLaunchNodes);
}

function UpdateNavNodes($navNodesCollection)
{
  if ($navNodesCollection -ne $null)
  {
    foreach($navNode in $navNodesCollection)
    {
        $navNode.Url = $navNode.Url.Replace('http://<My Root Site Url>','')
        $navNode.Update()
    }
  }
}

Write-Host -ForegroundColor red "STARTED"
ProcessWeb(Get-SPWeb -identity "http://<My Site Collection Url>")
Write-Host -ForegroundColor red "FINISHED"

No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...