Tìm kiếm trang web

SharePoint Online: Nhận-Đặt Giá trị Trường "Có/Không (Hộp kiểm)" bằng PowerShell


Yêu cầu: Nhận hoặc Đặt Giá trị Trường Có/Không trong SharePoint Online bằng PowerShell.

Trong SharePoint Online, trường “Có/Không (hộp kiểm)” cho phép người dùng chỉ ra lựa chọn. Điều này có thể hữu ích khi bạn cần theo dõi các giá trị boolean chẳng hạn như “trạng thái” từ người dùng. Bạn có thể đặt giá trị của trường này hoặc truy xuất giá trị của nó bằng PowerShell. Trong bài đăng này, chúng tôi sẽ hướng dẫn bạn cách sử dụng PowerShell để nhận và đặt giá trị của trường “Có/Không”.

PowerShell để nhận giá trị của trường Có/Không trong SharePoint Online:

Đây là cách bạn có thể đọc trường Có/Không theo chương trình bằng PowerShell.


#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"
 
#Set parameter values accordingly
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="IsActive" #Internal Name
$ListItemID="5"
 
#Get Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the Yes/No Field Value
$ListItem[$FieldName]

Cập nhật giá trị Trường Có/Không trong Danh sách SharePoint Online bằng PowerShell

Hãy cập nhật trường Có/Không trong danh sách SharePoint Online theo chương trình.


#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"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName="Projects"
$FieldName="IsActive" #Internal Name
$ListItemID="5"
 
#Get Credentials to connect
$Cred= Get-Credential
  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Get the List Item
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Update the Yes/No Field Value
$ListItem[$FieldName] = $True
$ListItem.update()
$Ctx.ExecuteQuery()

Write-host -f Green "Field Value Updated!"

PnP PowerShell để cập nhật giá trị trường “Có/Không” trong SharePoint Online

Để đặt giá trị cho các trường boolean “Có/Không”, hãy sử dụng Giá trị @{“YesNoFieldInternalName”=$True/$false} thông qua lệnh ghép ngắn Set-PnPListItem. Đây là một ví dụ:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$FieldName = "IsActive"
$ItemID = 1

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Update Yes/No Field Value
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = $True}

Bài viết liên quan