SharePoint Online: cree una columna de una sola línea de texto en la lista usando PowerShell
Requisito: PowerShell para agregar una sola línea de columna de texto a la lista de SharePoint Online.
¿Cómo agregar una sola línea de columna de texto a la lista de SharePoint Online?
La columna "Una sola línea de texto" en SharePoint Online almacena el valor del texto en una sola fila de hasta 255 caracteres. Podemos usar este tipo de campo cuando desee agregar valor de texto fluido como metadatos de una lista o biblioteca de documentos. Para agregar una sola línea de campo de texto a una lista, haga lo siguiente:
- Vaya a su sitio de SharePoint Online y navegue hasta la lista de destino en la que desea crear una nueva columna de una sola línea de texto.
- En la pestaña Lista, haga clic en el botón "Crear columna" en la cinta.
- Proporcione el Nombre a su nueva columna, especifique el tipo como "Línea única de texto".
Complete otros valores opcionales y haga clic en "Aceptar" para crear una columna de una sola línea de texto en la lista de SharePoint Online.
PowerShell para agregar una sola línea de columna de texto a la lista en SharePoint Online
Aquí está el script de PowerShell para crear una columna de texto de una sola línea en cualquier lista o biblioteca existente en SharePoint Online:
#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-SingleLineTextColumnToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $DisplayName,
[Parameter(Mandatory=$true)] [string] $Description,
[Parameter(Mandatory=$true)] [string] $IsRequired,
[Parameter(Mandatory=$true)] [string] $EnforceUniqueValues,
[Parameter(Mandatory=$true)] [string] $MaxLength
)
#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='Text' ID='{$FieldID}' Name='$Name' StaticName='$Name' DisplayName='$DisplayName' Description='$Description' Required='$IsRequired' EnforceUniqueValues='$EnforceUniqueValues' MaxLength='$MaxLength' />"
$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="ProjectCode"
$DisplayName="Project Code"
$Description="Enter the Project Code"
$IsRequired = "TRUE"
$EnforceUniqueValues="FALSE" #Must set Indexed="FALSE", if Unique="TRUE"
$MaxLength="100"
#Call the function to add column to list
Add-SingleLineTextColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -IsRequired $IsRequired -MaxLength $MaxLength -EnforceUniqueValues $EnforceUniqueValues
¿Cómo establecer el valor predeterminado para el campo?
Puede establecer el valor predeterminado agregando la etiqueta
$FieldSchema = "<Field Type='Text' Name='$Name' StaticName='$Name' DisplayName='$DisplayName'><Default>Project Code - XXXXX</Default></Field>"
PnP PowerShell para agregar campos a la lista en SharePoint Online
Aquí está el PowerShell para agregar una columna de lista de tipo Línea única de texto a la lista de SharePoint Online:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Team Projects"
$FieldTitle= "Project Status"
$FieldInternalName ="ProjectStatus"
$FieldType = "Text"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Add new field to list
Add-PnPField -List $ListName -DisplayName $FieldTitle -InternalName $FieldInternalName -Type $FieldType -Required -AddToDefaultView -ErrorAction Stop
Write-host -f Green "New Field '$FieldTitle' Added to the List!"
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
De manera similar, también podemos agregar una columna de texto a la lista de SharePoint Online desde el esquema XML.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName= "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='FALSE' EnforceUniqueValues = 'TRUE' Indexed='TRUE'></Field>"
#Add Text Field to list from XML
Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName
