Tìm kiếm trang web

Nhận-Đặt giá trị trường lựa chọn trong SharePoint bằng PowerShell


Trong SharePoint, trường Lựa chọn là trường mà bạn có thể chọn từ danh sách giá trị được xác định trước. Đôi khi, Bạn có thể cần truy xuất hoặc cập nhật giá trị cho trường Lựa chọn bằng PowerShell. Trong bài đăng trên blog này, chúng tôi sẽ chỉ cho bạn cách thực hiện điều đó.

Đây là bộ sưu tập tập lệnh PowerShell tiện lợi của tôi để đọc hoặc cập nhật các giá trị cột Lựa chọn trong SharePoint:

SharePoint: PowerShell để nhận giá trị trường lựa chọn

PowerShell để nhận các giá trị trường lựa chọn:


#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 để nhận giá trị trường nhiều lựa chọn

Chúng ta có thể nhận giá trị trường lựa chọn theo chương trình bằng cách sử dụng tập lệnh PowerShell này.


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: Đặt giá trị trường lựa chọn bằng PowerShell

Tập lệnh PowerShell này thêm giá trị vào cột lựa chọn.


#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(

Đặt giá trị Trường có nhiều lựa chọn bằng PowerShell:

Điều này đặt trường nhiều lựa chọn theo chương trình.


#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()

Điều này đặt giá trị của trường lựa chọn.

Cập nhật hộp kiểm trường lựa chọn (cho phép nhiều lựa chọn) Giá trị:

Đây là một phương pháp khác để đặt cột có nhiều lựa chọn.


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!"

Bài viết liên quan