SharePoint Online: Truy vấn CAML cho Trường Người hoặc Nhóm trong PowerShell
Yêu cầu: Truy vấn CAML SharePoint Online cho trường Người hoặc Nhóm
SharePoint Online: Truy vấn CAML để lọc trường người hoặc nhóm trong PowerShell
Trong SharePoint Online, trường Người hoặc Nhóm cho phép người dùng chọn một hoặc nhiều người hoặc nhóm từ trang SharePoint. CAML (Ngôn ngữ đánh dấu ứng dụng cộng tác) là ngôn ngữ truy vấn dựa trên XML được sử dụng trong SharePoint để truy vấn dữ liệu từ danh sách và thư viện. Trong bài viết này, chúng ta sẽ thảo luận cách tạo truy vấn CAML cho trường Người hoặc Nhóm trong PowerShell.
Giả sử chúng tôi muốn lọc và nhận tất cả các dự án trong đó giá trị trường người dùng “Người quản lý dự án” là một người cụ thể.
#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"
$UserID="salaudeen@crescent.com"
#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"]
}
Bộ lọc truy vấn SharePoint CAML theo tên người dùng:
Tập lệnh PowerShell ở trên lọc trường người dùng bằng thuộc tính UserID. Bạn cũng có thể lọc trường người dùng dựa trên tên hiển thị của người dùng:
<View Scope='RecursiveAll'>
<Query>
<Where>
<Eq>
<FieldRef Name='ProjectManager'/><Value Type='Text'>Salaudeen Rajack</Value>
</Eq>
</Where>
</Query>
</View>
Nhận các mục danh sách trong đó Trường người hoặc nhóm là người dùng hiện tại:
SharePoint CAML để lọc dựa trên người dùng hiện tại
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='ProjectManager'/><Value Type='Integer'><UserID /></Value>
</Eq>
</Where>
</Query>
</View>
Truy vấn CAML của SharePoint cho trường nhiều người dùng
Hãy lấy tất cả các mục danh sách trong đó một người dùng cụ thể được liệt kê trong cột bộ chọn người có nhiều giá trị “Thành viên dự án”.
<View Scope='RecursiveAll'>
<Query>
<Where>
<Includes>
<FieldRef Name='ProjectMembers' LookupId='TRUE'/><Value Type='User'>$UserID</Value>
</Includes>
</Where>
</Query>
</View>"
Làm theo các tập lệnh và ví dụ được cung cấp trong bài viết này, bạn có thể dễ dàng tạo truy vấn CAML cho trường Người hoặc Nhóm trong PowerShell và truy vấn dữ liệu từ danh sách SharePoint. Đây là một bài đăng khác dành cho SharePoint tại chỗ để lọc các trường người dùng bằng truy vấn CAML: Truy vấn SharePoint CAML để lọc theo trường người dùng trong PowerShell