Wednesday, October 15, 2014

Find and Replace URL Links from Hyperlink Columns in SharePoint

We had a SharePoint portal site called "Crescent Portal" with URL: After migrating from SharePoint 2010 to SharePoint 2013, we made a decision to replace both site name and URL as "Crescent Intranet" with URL "".

We understand there are plenty of lists and libraries hard-coded with the old site URL in its Hyperlink columns. Now, we got to find and replace those old links from all SharePoint lists and libraries.

PowerShell script to find and replace links in Hyperlink columns of SharePoint:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Replace-LinkFields($WebURL, $OldLinkURL, $OldLinkTitle, $NewLinkURL, $NewLinkTitle)
    #Get the Web
    $Web = Get-SPWeb $WebURL

    #Get all lists - Exclude System lists
    $ListCollection = $web.lists | Where-Object  { ($_.hidden -eq $false) -and ($_.IsSiteAssetsLibrary -eq $false) -and ($_.Author.LoginName -ne "SHAREPOINT\system") }
    #Iterate through each list
    foreach ($List in $ListCollection)
        #find all HyperLink fields in list
        $HyperlinkFields = @()
        foreach ($field in $list.Fields) 
            if ($field.TypeAsString -eq "URL") 
                $HyperlinkFields = $HyperlinkFields + $field.Title      
        write-host "Processing list at: $($web.url)/$($list.RootFolder.Url)"

        #Process all hyperlink fields found
        #Proceed with next list if Hyperlink field is not found in the list
        if($HyperlinkFields.Count -eq 0) { continue }

        foreach ($Item in $List.Items) 
            #Iterate through HyperLink fields
            foreach ($field in $HyperlinkFields) 
                #Get field value
                $FieldValue = $item[$field]
                #Skip nulls
                if($FieldValue -ne $null) 
                    # Check for OldLinkURL or OldLinkTitle
                    if( ($FieldValue.contains($OldLinkURL)) -or ($FieldValue.contains($OldLinkTitle)) )
                        #Replace the OLD URL with New URL
                        $Item[$field] = ($item[$field] -Replace $OldLinkURL,$NewLinkURL) 
                        #Replace OLD link title with new link title
                        $Item[$field] = ($item[$field] -Replace $OldLinkTitle,$NewLinkTitle) 

                        Write-host "Found and replaced a old link item at: $($web.Url)/$($list.RootFolder.Url) - Item id: $($"      

#Call the function to Replace Links in lists
Replace-LinkFields "" "" "Crescent Portal" "" "Crescent Intranet"
This script scans all lists and libraries of the given site and replaces old links. You can change the logic to process all sites in the web application to replace the old links across your web application.

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

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...