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

[玩转系统] 使用 PowerShell 进行 CAML 查询来筛选是/否(复选框)字段值

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

使用 PowerShell 进行 CAML 查询来筛选是/否(复选框)字段值


要求:CAML 查询在 PowerShell 中过滤是/否字段值。

用于筛选 SharePoint Online 中是/否字段值的 CAML 查询:

让我们使用 PowerShell 从 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
$FieldValue="1" #0 for FALSE
 
#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
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Define the CAML Query to filter Yes/No Field Value
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='$FieldName'/>
                <Value Type='Boolean'>$FieldValue</Value>
            </Eq>
        </Where>
    </Query>
</View>"

#Get All List Items matching the query
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

Write-host $ListItems.count 

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

取消回复欢迎 发表评论:

关灯