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

[玩转系统] SharePoint Online:PowerShell 中人员或组字段的 CAML 查询

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

SharePoint Online:PowerShell 中人员或组字段的 CAML 查询


要求:针对个人或组字段的 SharePoint Online CAML 查询

SharePoint Online:CAML 查询以在 PowerShell 中筛选人员或组字段

在 SharePoint Online 中,“人员”或“组”字段允许用户从 SharePoint 网站中选择一个或多个人员或组。 CAML(协作应用程序标记语言)是一种基于 XML 的查询语言,在 SharePoint 中用于查询列表和库中的数据。在本文中,我们将讨论如何在 PowerShell 中为人员或组字段创建 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"
$UserID="[email protected]"
 
#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 and User Objects
$List = $Ctx.Web.lists.GetByTitle($ListName)
$User = $Ctx.web.EnsureUser($UserID)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
$UserID = $User.Id

#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='ProjectManager' LookupId='TRUE'/><Value Type='User'>$UserID</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)
{ 
    Write-host $Item.id
    Write-host $Item["Title"]
}

SharePoint CAML 查询按用户名筛选:

上面的 PowerShell 脚本使用 UserID 属性过滤用户字段。您还可以根据用户的显示名称过滤用户字段:


<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='ProjectManager'/><Value Type='Text'>Salaudeen Rajack</Value>
            </Eq>
        </Where>
    </Query>
</View>

获取个人或组字段为当前用户的列表项:
SharePoint CAML 根据当前用户进行筛选


<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='ProjectManager'/><Value Type='Integer'><UserID /></Value>
            </Eq>
        </Where>
    </Query>
</View>

多用户字段的 SharePoint CAML 查询
让我们获取在多值人员选择器列“项目成员”下列出特定用户的所有列表项。


<View Scope='RecursiveAll'>
    <Query>
        <Where>
            <Includes>
                <FieldRef Name='ProjectMembers' LookupId='TRUE'/><Value Type='User'>$UserID</Value>
            </Includes>
        </Where>
    </Query>
</View>" 

按照本文中提供的脚本和示例,您可以轻松地在 PowerShell 中为人员或组字段创建 CAML 查询,并从 SharePoint 列表查询数据。这是 SharePoint 本地使用 CAML 查询筛选用户字段的另一篇文章:SharePoint CAML Query to Filter by User Field in PowerShell

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

取消回复欢迎 发表评论:

关灯