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

[玩转系统] SharePoint Online:使用 PowerShell 获取设置“是/否(复选框)”字段值

作者:精品下载站 日期:2024-12-14 14:34:25 浏览:17 分类:玩电脑

SharePoint Online:使用 PowerShell 获取设置“是/否(复选框)”字段值


要求:使用 PowerShell 在 SharePoint Online 中获取或设置“是/否”字段值。

在 SharePoint Online 中,“是/否(复选框)”字段允许用户指示选择。当您需要跟踪用户的“状态”等布尔值时,这会很有帮助。您可以设置此字段的值或使用 PowerShell 检索其值。在这篇文章中,我们将向您展示如何使用 PowerShell 获取和设置“是/否”字段的值。

[玩转系统] SharePoint Online:使用 PowerShell 获取设置“是/否(复选框)”字段值

PowerShell 获取 SharePoint Online 中“是/否”字段的值:

以下是如何使用 PowerShell 以编程方式读取“是/否”字段。


#Load SharePoint CSOM 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"
 
#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]

使用 PowerShell 更新 SharePoint Online 列表中的是/否字段值

让我们以编程方式更新 SharePoint Online 列表中的“是/否”字段。


#Load SharePoint CSOM 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"
 
#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 更新 SharePoint Online 中的“是/否”字段值

要设置“是/否”布尔字段的值,请通过 Set-PnPListItem cmdlet 使用 Values @{“YesNoFieldInternalName”=$True/$false}。这是一个例子:


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

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

取消回复欢迎 发表评论:

关灯