SharePoint Online: actualice el valor del campo de hipervínculo mediante PowerShell
Requisito: Actualice el valor de un campo de hipervínculo en SharePoint Online mediante PowerShell.
SharePoint Online PowerShell para actualizar el campo de hipervínculo
Los campos de hipervínculo se pueden utilizar para vincular documentos, elementos de lista, páginas y más. En algunos casos, es posible que necesite actualizar la URL del hipervínculo o el texto del vínculo para los elementos existentes en su lista de SharePoint Online. Esta publicación de blog le mostrará cómo actualizar el valor de un campo de hipervínculo en SharePoint utilizando el script de PowerShell.
Agreguemos un nuevo elemento de enlace a la lista de enlaces usando PowerShell:
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Add New Item to Link list
If($List -ne $Null)
{
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $List.AddItem($ListItemInfo)
$ListItem["Title"] = "Support Center"
#Set Hyperlink field properties
$Link = New-Object Microsoft.SharePoint.Client.FieldUrlValue
$Link.Url = "https://Crescent.sharepoint.com/sites/support"
$Link.Description = "Support Center URL"
#Update Hyperlink Field
$ListItem[$FieldName] = [Microsoft.SharePoint.Client.FieldUrlValue]$Link
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host "New Item Added to Link List!" -ForegroundColor Green
}
Buscar y reemplazar hipervínculos usando PowerShell:
De manera similar, podemos actualizar los valores de los campos de hipervínculo existentes usando PowerShell en SharePoint Online. Actualicemos todos los elementos de la lista de enlaces con la URL "/sites/support" con "/sites/supportV2".
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName = "Quick Links"
$FieldName = "URL"
#Hyperlink Field Values
$HyperLinkValue = "Support Center"
$HyperLinkURL = "/sites/support" #Provide FULL URL for External URLs
$HyperLinkNewURL = "/sites/supportV2"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define the CAML Query to Filter by Hyperlink value and Get List Item
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
<Query>
<Where>
<Eq >
<FieldRef Name='$FieldName' /><Value Type='URL'>$HyperLinkURL</Value>
</Contains >
</Eq>
</Query>
</View>"
#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Update the List Item's Hyperlink Field value
If($ListItems.Count -gt 0)
{
ForEach($Item in $ListItems)
{
#Get Hyperlink field value and update
$HyperLinkField = [Microsoft.SharePoint.Client.FieldUrlValue]$Item[$FieldName]
$HyperLinkField.Url = $HyperLinkNewURL
$HyperLinkField.Description = $HyperlinkDescription
#Update Hyperlink Field
$Item[$FieldName] = $HyperLinkField
$Item.Update()
$Ctx.ExecuteQuery()
Write-host "Hyperlink Field Value Updated in Link List!" -ForegroundColor Green
}
}
Else
{
Write-host "Hyperlink Field Value '$($HyperLinkURL)' Not Found in Link List!" -ForegroundColor Yellow
}
PnP PowerShell para actualizar la columna de hipervínculo en SharePoint Online:
Para configurar los valores del campo Hipervínculo o Imagen usando el cmdlet PnP PowerShell Set-PnPListItem, use: formato @{“Hyperlink”=“https://URL, LinkText”}.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Projects"
$ItemID = 1
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Update Hyperlink column value using PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"CompanyWebsite"= "https://www.CrescentTech.com, Crescent Technologies Inc."}
Este script se conecta a SharePoint Online mediante PnP PowerShell y actualiza la columna de hipervínculo "Sitio web de la empresa" en un elemento de la lista con ID "1".
Buscar y reemplazar enlaces en la lista de SharePoint Online usando PnP PowerShell
¿Qué tal buscar y reemplazar una cadena específica en la URL o Descripción de un valor de campo de hipervínculo particular en todos los elementos de la lista usando PnP PowerShell? Es posible que con frecuencia necesite actualizar los enlaces a recursos o ubicaciones a medida que crecen y cambian. Tuve que buscar y reemplazar todas las instancias de un valor antiguo "horizonte" con un nuevo valor "media luna" en una lista de SharePoint Online.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Projects"
$ColumnName = "Project_x0020_Reference" #Internal Name of the Field
$OldString = "horizon" #Set it in lower case
$NewString = "crescent"
#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All List Items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
#Iterate through all items in the list
ForEach ($Item in $ListItems)
{
$Flag = $false
$URL = $Item.FieldValues[$ColumnName].URL.ToLower()
#Check if the URL has the old text
If($URL.contains($OldString))
{
$URL = $URL -Replace $OldString,$NewString
$Flag = $True
}
#check the description of the Hyperlink
$Description = $Item.FieldValues[$ColumnName].Description.ToLower()
If($Description.contains($OldString))
{
$Description = $Description -Replace $OldString,$NewString
$Flag = $True
}
#update the Hyperlink field if URL or Description has the old string
If($Flag -eq $True)
{
#Frame the URL field value
$Hyperlink = New-Object Microsoft.SharePoint.Client.FieldUrlValue
$Hyperlink.Url= $URL
$Hyperlink.Description= $Description
#Update Hyperlink field value
Set-PnPListItem -List $ListName -Identity $Item.Id -Values @{$ColumnName = [Microsoft.SharePoint.Client.FieldUrlValue]$Hyperlink} | Out-Null
Write-host -f Green "List Item $($Item.ID) Updated!"
}
}
Si sigue los pasos descritos en esta guía, podrá obtener rápida y fácilmente el enlace que desea actualizar, actualizar la URL y guardar los cambios.
