SharePoint Online: Agregar columna de sitio a la lista usando PowerShell
Requisito: Agregue una columna de sitio existente a la lista o biblioteca de SharePoint mediante PowerShell.
¿Cómo agregar una columna de sitio a la lista de SharePoint Online?
Las columnas del sitio en SharePoint brindan una gran reutilización sin tener que recrear las mismas columnas varias veces. Una vez creados en el sitio de nivel superior, podemos utilizarlos para almacenar metadatos en cualquier número de listas y bibliotecas bajo la jerarquía. Para agregar una columna de sitio a la lista de SharePoint, siga estos pasos:
- Vaya a Configuración de lista >> En Columnas, haga clic en el enlace "Agregar desde columnas del sitio existente".
De las columnas de sitio disponibles, seleccione las columnas de sitio requeridas y haga clic en el botón Agregar.
- Haga clic en Aceptar para guardar los cambios.
Agreguemos una columna de sitio a la lista de SharePoint Online usando PowerShell.
Agregar columna de sitio a la lista o biblioteca con PowerShell:
Este script CSOM de PowerShell agrega una columna de sitio existente, "Departamento", a la lista "Proyectos".
#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"
#Variables
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$SiteColumnName="Department"
$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
$Web = $Ctx.web
#Get the list
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#Get the Site column
$Field = $Web.Fields.GetByTitle($SiteColumnName)
#Add the site column to the list
$List.Fields.Add($Field)
$ctx.ExecuteQuery()
Write-host "Site Column Added to the List Successfully!" -f Green
PowerShell para agregar una columna de sitio a la lista de SharePoint:
Agreguemos algo de manejo de errores al código anterior para manejar escenarios, como:
- ¿Qué pasa si la columna del sitio proporcionada no existe?
- ¿Qué pasa si la columna del sitio dado ya está agregada a la lista?
- ¿Qué pasa si la lista dada no existe? ¿O qué pasa si las credenciales proporcionadas no son válidas? Etc.
#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"
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$SiteColumnName="Department"
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
$Web = $Ctx.web
#Get the Site column from web
$SiteColumns = $Web.Fields
$Ctx.Load($SiteColumns)
$Ctx.ExecuteQuery()
$SiteColumn = $SiteColumns | Where {$_.Title -eq $SiteColumnName}
#Check if given site column exists
if($SiteColumn -eq $Null)
{
Write-host "Site Column $SiteColumnName doesn't exists!" -f Yellow
}
else
{
#Get the list
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#Check if the Filed exist in list already
$Fields = $List.Fields
$Ctx.Load($List)
$Ctx.Load($Fields)
$Ctx.ExecuteQuery()
$Field = $Fields | where {$_.Title -eq $SiteColumnName}
if($Field -ne $NULL)
{
Write-host "Column Name $SiteColumnName already exists in the list!" -f Yellow
}
else
{
#Add the site column to the list
$NewColumn = $List.Fields.Add($SiteColumn)
$ctx.ExecuteQuery()
Write-host "Site Column Added to the List Successfully!" -f Green
}
}
}
Catch {
write-host -f Red "Error Adding Site Column to List!" $_.Exception.Message
}
Aquí hay otro ejemplo:
#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"
#Function to Add Fields to List
Function Add-SPOSiteColumnToList([Microsoft.SharePoint.Client.List]$List, [GUID]$FieldID)
{
#Get Fields from List
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
#Get the Site Column from Web
$SiteColumn = $List.ParentWeb.AvailableFields.GetById($FieldID)
$Ctx.Load($SiteColumn)
$Ctx.ExecuteQuery()
#Check if the Field exist in list
$ListField = $List.Fields | where {$_.ID -eq $SiteColumn.Id}
if($ListField -eq $NULL)
{
#Add the site column to the list
$NewColumn = $List.Fields.Add($SiteColumn)
$ctx.ExecuteQuery()
Write-host "Site Column '$($SiteColumn.Title)' Added to the List Successfully!" -f Green
}
else
{
Write-host "Site Column '$($SiteColumn.Title)' already exists in the list!" -f Yellow
}
}
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Team Documents"
#Setup Credentials to connect
$Cred = Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Add Rating Fields to List
$AverageRatingFieldID = [guid]"5a14d1ab-1513-48c7-97b3-657a5ba6c742"
$RatingCountFieldID = [guid]"b1996002-9167-45e5-a4df-b2c41c6723c7"
#Call the function to Add Site column to List
Add-SPOSiteColumnToList -List $List -FieldID $AverageRatingFieldID
Add-SPOSiteColumnToList -List $List -FieldID $RatingCountFieldID
PnP PowerShell para agregar una columna de sitio a la lista
A continuación se explica cómo agregar una columna de sitio a la lista de SharePoint Online usando PnP PowerShell:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Team Projects"
$ColumnName= "ProjectStartDate" #Internal Name
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Add Existing Site column to list
Add-PnPField -List $ListName -Field $ColumnName -ErrorAction Stop
Write-host "Site Column '$ColumnName' Added to List Successfully!" -f Green
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online PowerShell para agregar una columna a la lista
Si está buscando SharePoint Online PowerShell para agregar una columna a una lista, como "Línea única de texto" o "Elección", etc. usar :
- SharePoint Online: agregue una sola línea de campo de texto a la lista usando PowerShell
- SharePoint Online: agregue varias líneas de columna de texto a la lista usando PowerShell
- SharePoint Online: agregar columna calculada a la lista usando PowerShell
- SharePoint Online: agregue un hipervínculo o una columna de imagen a la lista usando PowerShell
- SharePoint Online: agregar columna de búsqueda a la lista usando PowerShell
- SharePoint Online: ¿Cómo agregar un campo de geolocalización (Bing Maps) a la lista usando PowerShell?
- SharePoint Online: agregar columna numérica a la lista usando PowerShell
- SharePoint Online: agregar campo de elección a la lista usando PowerShell
- SharePoint Online: Agregar columna de moneda a la lista usando PowerShell
- SharePoint Online: agregue una columna de metadatos administrados a la lista usando PowerShell
- SharePoint Online: agregue la columna de casilla de verificación "Sí/No" a la lista usando PowerShell
- SharePoint Online: Agregar columna de persona o grupo (selector de personas) a la lista usando PowerShell
- SharePoint Online: cree una columna de fecha y hora en la lista usando PowerShell
- SharePoint Online: agregue una columna de metadatos administrados a la lista usando PowerShell
