Wednesday, January 30, 2013

Find and Replace Old Link URLs in Quick Launch, Top Navigation Menus during Migration

Found so many hard-coded links (Absolute links) in SharePoint Quick launch and in Top navigation bar. This PowerShell script, Scans and Replaces the provided Old URL with the New URL all over the SharePoint web Application.

PowerShell Script to Find and Replace Quick Launch & Top Navigation Links:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Old & New URLs
$OldURL = "http://moss2007.crescent.com"
$NewURL = "http://sharepoint2010.crescent.com"

#Get all sites of the web application
$webs = Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 
#Iterate through webs
foreach ($web in $webs)
{
 #Get the Quick Launch Bar Nodes
 $QuickLaunchNodes = $Web.Navigation.QuickLaunch 
 #For Top Navigation use: $Web.Navigation.TopNavigationBar . TOp Nav may or May not have child nodes

 #Iterate through each Parent nodes of Quick launch
 foreach ($parentNode in $QuickLaunchNodes)
 { 
    if($parentNode.Url -match $OldURL)  #if you want to match Link text, use: $parentNode.Title
 {
        $parentNode.Url = $parentNode.Url.Replace($OldURL,$NewURL)
        $parentNode.Update()
  Write-Host "$OldURL Updated!"
    }
 #Get the Child Nodes
 $childNodes = $parentNode.Children
 
 #Iterate through child nodes
 foreach ($childNode in $childNodes)
  {
         if($childNode.Url -match $OldURL)
   {
             $childNode.Url = $childNode.Url.Replace($OldURL,$NewURL)
             $childNode.Update()
    Write-Host "$OldURL Updated!"
   }
     }
 }
$Web.Dispose()
 
This script may even helpful when you move your SharePoint web application to different URL.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

4 comments :

  1. Hi I know this is a old article but was wondering if you have a similar script to do the same on MOSS 2007. I have an old MOSS 2007 site that i have moved to a new server that will remain MOSS 2007 as an archive but now need to update all the quick launch items as some are hard coded. My issue is that MOSS 2007 doesnt allow for the PSSnapin If you could show me an example it would be hugely appreciated.

    ReplyDelete
    Replies
    1. Edelmundo,
      You can use the above code with MOSS 2007 also. Only change is: You have to imitate SharePoint PowerShell cmdlets like "Get-SPWebApplication". Refer this article to Use PowerShell in SharePoint 2007 : How to use PowerShell with MOSS 2007

      Delete
    2. Thanks for the quick reply, i have tried as you mention to use the above and creating a seperate PS script for the functions so one for get site, one for get web applicaton and one for spweb i have created them with the functions as shown in the link you included. Then i have included them in my new fixupurl script but it doesent work here is the fix up script
      . "D:\PS\getSPWebApplications.ps1"
      . "D:\PS\getSPWeb.ps1"
      . "D:\PS\getSPSite.ps1"

      #Set Old & New URLs
      $OldURL = "http://myoldSite"
      $NewURL = "/"

      #Get all sites of the web application
      $webs = Get-SPWebApplication "http://mynewsite" | Get-SPSite -Limit All | Get-SPWeb -Limit All

      #Iterate through webs
      foreach ($web in $webs)
      {
      #Get the Quick Launch Bar Nodes
      $QuickLaunchNodes = $Web.Navigation.QuickLaunch
      #For Top Navigation use: $Web.Navigation.TopNavigationBar . TOp Nav may or May not have child nodes

      #Iterate through each Parent nodes of Quick launch
      foreach ($parentNode in $QuickLaunchNodes)
      {
      if($parentNode.Url -match $OldURL) #if you want to match Link text, use: $parentNode.Title
      {
      $parentNode.Url = $parentNode.Url.Replace($OldURL,$NewURL)
      $parentNode.Update()
      Write-Host "$OldURL Updated!"
      }
      #Get the Child Nodes
      $childNodes = $parentNode.Children

      #Iterate through child nodes
      foreach ($childNode in $childNodes)
      {
      if($childNode.Url -match $OldURL)
      {
      $childNode.Url = $childNode.Url.Replace($OldURL,$NewURL)
      $childNode.Update()
      Write-Host "$OldURL Updated!"
      }
      }
      }
      }
      $Web.Dispose()

      but it keeps throwning the error

      New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.SPSite.
      At D:\PS\getSPSite.ps1:6 char:22
      + return new-Object <<<< Microsoft.SharePoint.SPSite($url)
      + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
      + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

      The term 'Get-SPWebApplication' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
      At D:\PS\fixUpURLS.ps1:10 char:29
      + $webs = Get-SPWebApplication <<<< "http://inttest.capitasymonds.co.uk" | Get-SPSite -Limit All | Get-SPWeb -Limit All
      + CategoryInfo : ObjectNotFound: (Get-SPWebApplication:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException

      You cannot call a method on a null-valued expression.
      At D:\PS\fixUpURLS.ps1:43 char:13
      + $Web.Dispose <<<< ()
      + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull

      Any Ideas where im going wrong ?

      Delete
    3. Any way to show where it made the changes? vs. just outputting oldurl Updated!

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...