I've written some scripts that were supposed to help me correct links manually added to the navigation of a site. Basically I'm just doing a find and replace in all of the URLs. I've figured out how to access the links and update their URLs but the changes immediately roll back. I've modified my original function by adding console feedback to indicate what's happening and where, as well as an alternate implementation that should also work but suffers the same problem.
function fixLinks {
<#
.SYNOPSIS
fixes broken links
.DESCRIPTION
Searches through all navigation links in the Quick launch menu, and corrects all urls based on string replacement.
.EXAMPLE
fixLinks Get-SPweb "http://intranet/clients" "/All Client Sites/" "/clients/"
.PARAMETER SPSite
The sharepoint site to fix.
.PARAMETER badUrlString
the bad string that needs to be replaced from the url
.PARAMETER goodUrlString
the good string that needs to be replace the bad one in the url
#>[CmdletBinding()]
param
(
[Parameter(Position=1,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the address of the sharepoint site collection/ site/ subsite you would like to fix?')]
[Alias('site')]
[Microsoft.SharePoint.SPWeb]$SPSite,
[Parameter(Position=2,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the old string that needs to be replaced from link urls on the site?')]
[Alias('bad')]
[String]$badUrlString,
[Parameter(Position=3,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the new string to correct link urls on the site?')]
[Alias('good')]
[String]$goodUrlString
)
BEGIN {}
PROCESS {
$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($SPSite);
foreach($link in $SPPubWeb.Navigation.CurrentNavigationNodes) {
write-verbose ("checking link url: " + $link.url)
$url = $link.url -replace $badUrlString, $goodUrlString
if ([string]::Compare($url, $link.url, $True) -ne 0)
{
write-output("fixing url ("+$link.url+") to ("+$url+")")
$link.url = $url
write-verbose ("the link was correctly updated see: "+$link.url)
}
}
write-verbose ("the links are already reverted now!")
foreach($link in $SPPubWeb.Navigation.CurrentNavigationNodes) {
write-verbose ($link.url)
}
$SPPubWeb.update()
write-verbose("-------------------------------------------------------------------------------");
write-verbose("Same thing occurs directly using a Microsoft.SharePoint.SPWeb");
foreach($link in $SPSite.Navigation.QuickLaunch) {
write-verbose ("checking link url: " + $link.url)
$url = $link.url -replace $badUrlString, $goodUrlString
if ([string]::Compare($url, $link.url, $True) -ne 0)
{
write-output("fixing url ("+$link.url+") to ("+$url+")")
$link.url = $url
write-verbose ("the link was correctly updated see: "+$link.url)
}
}
write-verbose ("the links are already reverted now!")
foreach($link in $SPSite.Navigation.QuickLaunch) {
write-verbose ($link.url)
}
$SPSite.update()
}
END{}
}
This is SharePoint 2010 just for clarification. I have been staring at this for too long. Any tips would be greatly appreciated.