Buscar y reemplazar enlaces URL desde columnas de hipervínculos en SharePoint
Requisito:
Teníamos un portal de SharePoint llamado “Crescent Portal” con URL: https://portal.crescent.com. Después de migrar de SharePoint 2010 a SharePoint 2013, decidimos reemplazar tanto el nombre del sitio como la URL como "Crescent Intranet" por la URL "https://intranet.crescent.com".
Entendemos que hay muchas listas y bibliotecas codificadas con la URL del sitio anterior en sus columnas Hipervínculo. Tenemos que buscar y reemplazar esos enlaces antiguos de todas las listas y bibliotecas de SharePoint.
Script de PowerShell para buscar y reemplazar enlaces en columnas de hipervínculo de 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)
$Item.update()
Write-host "Found and replaced a old link item at: $($web.Url)/$($list.RootFolder.Url) - Item id: $($item.id)"
}
}
}
}
}
}
#Call the function to Replace Links in lists
Replace-LinkFields "https://Intranet.crescent.com/" "https://portal.crescent.com" "Crescent Portal" "https://intranet.crescent.com" "Crescent Intranet"
Este script escanea todas las listas y bibliotecas del sitio determinado y reemplaza los enlaces antiguos. Puede cambiar la lógica para procesar todos los sitios en la aplicación web para reemplazar los enlaces antiguos en su aplicación web.