Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
Whats your question ? –  Muhammad Raja Feb 26 '13 at 20:06
    
Why do the changes roll back, or how to stop them from doing so? –  Stephen Lujan Feb 26 '13 at 21:41

1 Answer 1

up vote 0 down vote accepted

Try to add a $link.Update() after the $link.url = $url

share|improve this answer
    
$link doesn't have an update method. Adding $SPPubWeb.update() there is one thing I tried but it didn't appear to work for me. Moreover if it did make a difference I would be surprised because if you were required to update after each alteration, why have an update method at all? Thank you very much for the suggestion! –  Stephen Lujan Feb 26 '13 at 21:39
    
Just to clarify $link is a Microsoft.SharePoint.Navigation.SPNavigationNode in either loop –  Stephen Lujan Feb 26 '13 at 22:02
    
hmmm. following the documentation SPNavigationNode should have an Update method: msdn.microsoft.com/en-us/library/… –  yannisgu Feb 27 '13 at 19:50
    
You're absolutely right. I tried it again and now its working! I initially tried $link.update() on a hunch without even consulting the api and it threw me an error about missing that member. –  Stephen Lujan Feb 27 '13 at 21:46
    
it was possibly just the casing you used that caused the missing member error. Update() and update() are different methods (when referencing sharepoint plugins). –  Mauro Feb 28 '13 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.