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

[玩转系统] SharePoint Online:PowerShell 中选择字段的 CAML 查询

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

SharePoint Online:PowerShell 中选择字段的 CAML 查询


要求:使用 PowerShell 对 SharePoint Online 中的选择字段进行 CAML 查询。

获取选择字段等于特定值的列表项:

让我们从项目列表中获取所有项目,其中“优先级”选择字段值为“中”。


#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"
 
#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 Object
$List = $Ctx.Web.lists.GetByTitle($ListName)

#Define the CAML Query - sharepoint caml query choice field
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Priority' /><Value Type='Choice'>(3) High</Value>
            </Eq>
        </Where>
    </Query>
</View>"

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

Write-host "Total Number of Items:"$ListItems.count

#Loop through each List Item
ForEach($Item in $ListItems)
{
    #Do something
    Write-host $Item["Title"]
}

用于多项选择的 SharePoint CAML

让我们获取优先级、多选列同时选择“高”和“中”值的所有项目。


$Query.ViewXml = "@
<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <And>
                <Contains>
                    <FieldRef Name='Priority' /><Value Type='MultiChoice'>High</Value>
                </Contains>
                <Contains>
                    <FieldRef Name='Priority' /><Value Type='MultiChoice'>Medium</Value>
                </Contains>
            </And>
        </Where>
    </Query>
</View>"

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

取消回复欢迎 发表评论:

关灯