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

[玩转系统] 使用 PowerShell 在 SharePoint 中获取-设置选择字段值

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

使用 PowerShell 在 SharePoint 中获取-设置选择字段值


在 SharePoint 中,选择字段是您可以从预定义值列表中进行选择的字段。有时,您可能需要使用 PowerShell 检索或更新 Choice 字段的值。在这篇博文中,我们将向您展示如何做到这一点。

以下是我收集的精美 PowerShell 脚本,用于读取或更新 SharePoint 中的 Choice 列值:

[玩转系统] 使用 PowerShell 在 SharePoint 中获取-设置选择字段值

SharePoint:PowerShell 获取选择字段值

PowerShell 获取选择字段值:


#Define URL and List name variables 
$WebURL="https://intranet.crescent.com/"
$ListName ="Projects"

$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)

#Get the 1st item stored in the list
$Item = $list.items[0]

#Get the formatted value of "Department" choice column
Write-host "Choice Field Value:" $item.GetFormattedValue("Department")

PowerShell 获取多个选择字段值

我们可以使用此 PowerShell 脚本以编程方式获取选择字段值。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$WebURL="https://intranet.crescent.com/"
$ListName ="Projects"
$MultiChoiceFieldName="Category"

$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)
#Get the 1st item stored in the list
$Item = $List.items[0]

#Get the multiple choice field 
$MultiChoiceValues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue($Item[$MultiChoiceFieldName])

#Print Each Value
For($I=0; $I -lt $MultiChoiceValues.count; $I++)
{            
 write-host $MultiChoiceValues[$I]
}

SharePoint:使用 PowerShell 设置选择字段值

此 PowerShell 脚本为选择列添加了值。


#Define URL and List name variables 
$WebURL="https://sharepoint.crescent.com/sites/Operations/"
$ListName ="List Versions"

#Get the Web and Lists objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]

#Add new Get the 1st item stored in the list
$Item = $list.AddItem()
$item["Title"] = "Test001" #Set Mandatory title field
#sharepoint 2010 powershell update choice field
$item["Department"] = "Sales"
$item.Update(

使用 PowerShell 设置多选字段值:

这以编程方式设置多选字段。


#Add new Get the 1st item stored in the list
$Item = $list.AddItem()
$item["Title"] = "Test009"

#Set Choice values
$choicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$choicevalues.Add("Sales")            
$choicevalues.Add("Purchase")    

$list.Fields["Department"].ParseAndSetValue($item,$choicevalues)

#Commit changes
$item.Update()

这设置了选择字段的值。

更新选择字段复选框(允许多项选择)值:

这是设置多选列的另一种方法。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Define URL and List name variables 
$WebURL="https://portal.crescent.com/sites/ops"
$ListName ="News"
 
#Get the Web and List and Item objects
$web = Get-SPWeb $WebURL
$List = $web.Lists[$ListName]
$Item = $List.GetItemById(1)

#Set Choice values
$choicevalues = New-Object Microsoft.SharePoint.SPFieldMultiChoiceValue
$choicevalues.Add("News")            
$choicevalues.Add("Flash News")    

#sharepoint Powershell to update choice field
$item["Category"] = $Choicevalues
$item.Update()

Write-host "Choice field values updated for item!"

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

取消回复欢迎 发表评论:

关灯