by

How To: Fix relative broken links in SharePoint (using PowerShell)

Scenario: There is project website in the production environment. How can we replicate that same environment for a test/development environment?
One of the options would be to backup and restore the whole site via SharePoint granular backup. However if you have a domain name specific for test (something like http://myprojectTEST.com) then you have to adjust the links that will likely come broken.
sharepoint-broken-links-migration_thumb[2]
The PowerShell script below address this case. It works similar to a string replace; the difference here is that it works in the navigation links.
function fix-quicklaunch-links([string]$URL, [string]$oldString, [string]$newString) 
{ 
    $errorFileName = "c:\temp\fix-quicklaunch-links-ERROR.txt" 
    $successFileName = "c:\temp\fix-quicklaunch-links-SUCCESS.txt" 

    $site = Get-SPSite $URL 

    $totalWebs = $site.AllWebs.Count – 1
    foreach($web in $site.AllWebs) 
    { 
        Write-Host 
        Write-Host -fore white -back blue "Processing web: " $web.Url 
        $nav = $web.Navigation 
        $quicklaunch = $nav.QuickLaunch 

        $totalQuickLaunch = $nav.QuickLaunch.Count - 1 
        for ($i=0; $i -le $totalQuickLaunch; $i++) 
        { 
            $mainlink = $quicklaunch[$i] 
            $linkurl=$mainlink.Url 

            if ($linkurl.Contains($oldString)) 
            { 
                try 
                { 
                    $mainlink.Url = $linkurl.Replace($oldString, $newString) 
                    $mainlink.Update() 
                    write $linkurl | Out-File -filepath $successFileName -append 
                    write-host -fore green -back green "." -nonewline 
                } 
                catch 
                { 
                    write $linkurl $_.Exception.Message | Out-File -filepath $errorFileName -append 
                    write-host -fore red -back red "." -nonewline 
                } 
            } 
            $totalChildren = $mainlink.Children.Count - 1 

            for ($j=0; $j -le $totalChildren; $j++) 
            { 
                $child = $mainlink.Children[$j] 
                $childurl = $child.Url 

                if ($childurl.Contains($oldString)) 
                { 
                    try 
                    { 
                        $child.Url = $childurl.Replace($oldString, $newString) 
                        $child.Update() 
                        write $childurl | Out-File -filepath $successFileName -append 
                        write-host -fore green -back green "." -nonewline 
                    } 
                    catch 
                    { 
                        write $childurl $_.Exception.Message | Out-File -filepath $errorFileName -append 
                        write-host -fore red -back red "." -nonewline 
                    } 
                } 
            } 
        } 

        $web.Update() 
    } 

    $site.Dispose() 

    $finaldate = Get-Date 
    write-host 
    write-host -fore white -back blue "End of processing ("  $finaldate ")"
}
 





so if you are migrating from http://myprojectsite to http://myprojectsiteTEST you would call the function as:


fix-quicklaunch-links “http://myprojectsite” “http://myprojectsite/” “http://myprojectsiteTEST/”


The script will run across the site you specify in the parameters and it will traverse all the sub sites and navigation links under it. At the end it will generate 2 log files in the c:\temp folder with the errors and successes during the process.


I hope it helps someone with this issue.

Cheers,

By