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:

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...