SharePoint Online: actualice las opciones en el campo de elección mediante PowerShell
Requisito: Actualizar las opciones de campo de elección en SharePoint Online mediante PowerShell.
SharePoint Online: PowerShell para actualizar el campo de elección
Como administrador, es posible que deba actualizar el campo de elección en la lista de SharePoint Online. La actualización de las opciones del campo de elección se puede realizar a través de la interfaz de usuario del navegador web, pero si desea automatizar el proceso, PowerShell proporciona una manera de hacerlo. Esta publicación le mostrará cómo usar PowerShell para actualizar opciones en un campo de elección.
#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 for Processing
$SiteUrl = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$FieldName="Category"
#Define Choices
$Choices = @("Growth Capital","Private Investment","Development", "Start up", "Real Estate")
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up 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()
#Get the field
$Field = $List.Fields.GetByInternalNameOrTitle($FieldName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()
#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldMultiChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
$Ctx.ExecuteQuery()
#$choiceField.Choices.Clear()
$ChoiceField.Choices = $Choices
$ChoiceField.UpdateAndPushChanges($True)
$Ctx.ExecuteQuery()
Write-host "Choice Field has been Updated!" -ForegroundColor Green
Si desea agregar opciones existentes, use lo siguiente:
$ChoiceField.Choices += $Choices
Esto agrega valor a las opciones existentes del campo de elección.
PnP PowerShell para agregar opciones a una columna de opciones en SharePoint Online
¿Cómo se obtienen todas las opciones de un campo de elección en la lista de SharePoint Online?
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Field
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName
#get all choices of the choice field
$Field.TypedObject.Choices
Ahora, actualicemos el campo de elección usando PnP PowerShell:
#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
$FieldInternalName = "Status"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext
#Get the Field to update
$Field = Get-PnPField -Identity $FieldInternalName -List $ListName
#Cast the field to Choice Field
$ChoiceField = New-Object Microsoft.SharePoint.Client.FieldChoice($Ctx, $Field.Path)
$Ctx.Load($ChoiceField)
Invoke-PnPQuery
#Add a Choice Value
$ChoiceField.Choices += "Dropped"
$ChoiceField.UpdateAndPushChanges($True)
Invoke-PnPQuery
Aquí hay otra publicación sobre cómo obtener y configurar valores de campos de elección usando PowerShell: ¿Cómo obtener/establecer valores de campos de elección en SharePoint Online usando PowerShell?
