当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 更新选择字段中的选项

作者:精品下载站 日期:2024-12-14 14:31:02 浏览:16 分类:玩电脑

SharePoint Online:使用 PowerShell 更新选择字段中的选项


要求:使用 PowerShell 更新 SharePoint Online 中的选择字段选择。

[玩转系统] SharePoint Online:使用 PowerShell 更新选择字段中的选项

SharePoint Online:PowerShell 更新选择字段

作为管理员,您可能需要更新 SharePoint Online 列表中的选择字段。更新选择字段的选项可以通过 Web 浏览器用户界面完成,但如果您想自动化该过程,PowerShell 提供了一种方法。这篇文章将向您展示如何使用 PowerShell 更新选择字段中的选项。


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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

如果您想附加到现有选项,请使用以下命令:


$ChoiceField.Choices += $Choices  

这为选择字段的现有选择增加了价值。

PnP PowerShell 将选项添加到 SharePoint Online 中的选项列

如何获取 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

现在,让我们使用 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

这是关于使用 PowerShell 获取和设置选择字段值的另一篇文章:如何使用 PowerShell 在 SharePoint Online 中获取/设置选择字段值?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯