Búsqueda de sitios web

SharePoint Online: agregue un hipervínculo o una columna de imagen a la lista usando PowerShell


Requisito: Agregue una columna de hipervínculo a la lista de SharePoint Online.

¿Cómo crear un hipervínculo o una columna de imagen en una lista usando PowerShell?

La columna Hipervínculo o Imagen en SharePoint Online se utiliza para almacenar direcciones URL de direcciones web o imágenes. El tipo de campo se puede configurar para contener una URL o una imagen. Estos son los pasos para agregar un campo de hipervínculo a la lista de SharePoint Online:

  1. Vaya a su sitio de SharePoint Online y navegue hasta la lista de destino en la que desea agregar un hipervínculo o una columna de imagen.
  2. En la pestaña Lista, haga clic en el botón "Crear columna" en la cinta.
  3. Proporcione el Nombre a su nueva columna, especifique el tipo como "Hipervínculo o Imagen". Seleccione el formato de la URL como Hipervínculo o Imagen en el menú desplegable "Formatear URL como".

  4. Complete otros valores opcionales, como Descripción del campo, Obligatorio y Agregar a la configuración de vista predeterminada. Haga clic en "Aceptar" para crear un campo Hipervínculo o Imagen en la lista de SharePoint Online.

PowerShell para agregar hipervínculo o columna de imagen a la lista de SharePoint Online:

Aquí está el script de PowerShell para agregar un campo de hipervínculo a la lista de SharePoint Online. Simplemente configure los parámetros en consecuencia y ejecute el script.


#Load SharePoint CSOM 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"

#Custom function to add column to list
Function Add-HyperLinkPictureColumnToList()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $Name,
        [Parameter(Mandatory=$true)] [string] $DisplayName,
        [Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
        [Parameter(Mandatory=$false)] [string] $IsRequired = "FALSE",
        [Parameter(Mandatory=$false)] [string] $Format ="Hyperlink"
    )

    #Generate new GUID for Field ID
    $FieldID = New-Guid

    Try {
        $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)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the column exists in list already
        $Fields = $List.Fields
        $Ctx.Load($Fields)
        $Ctx.executeQuery()
        $NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
        if($NewField -ne $NULL)  
        {
            Write-host "Column $Name already exists in the List!" -f Yellow
        }
        else
        {
            #Define XML for Field Schema
            $FieldSchema = "<Field Type='URL' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' Required='$IsRequired' Format='$Format' />"
            $NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
            $Ctx.ExecuteQuery()    

            Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding Column to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="ProjectReference"
$DisplayName="Project Reference"
$Description="Enter the Project Reference"
$Format="Hyperlink" #or "Image"

#Call the function to add column to list
Add-HyperLinkPictureColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Format $Format 

PnP PowerShell para agregar una columna de URL a la lista de SharePoint Online

Utilice el cmdlet Add-PnPField en PnP PowerShell con el tipo "URL" para crear un campo de hipervínculo en la lista en línea de SharePoint:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Add Hyperlink Field to list
Add-PnPField -Type URL -List $ListName -DisplayName "Company Website" -InternalName "CompanyWebsite" -AddToDefaultView

De manera similar, podemos crear un campo de hipervínculo a partir del esquema XML.


#Define XML Schema for URL Field
$FieldXML= "<Field Type='URL' Name='CompanyWebsite' ID='$([GUID]::NewGuid())' DisplayName='Company Website' Format='Hyperlink' ></Field>"

#Add URL Field to list
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

Establezca el atributo Formato='Imagen' para el campo Imagen.

Artículos relacionados