SharePoint Online: Đọc/Cập nhật giá trị Trường Người hoặc Nhóm (Bộ chọn Người) bằng PowerShell
Yêu cầu: Nhận hoặc Đặt Giá trị Trường “Người hoặc Nhóm” trong SharePoint Online bằng PowerShell.
Trường người hoặc Nhóm cung cấp danh sách những người và nhóm có thể tìm kiếm được khi thêm hoặc chỉnh sửa một mục. Nó hoạt động giống như một trường tự động hoàn thành khi bạn nhập bốn ký tự đầu tiên của tên người dùng. Đây là bộ sưu tập tập lệnh PowerShell của tôi để truy xuất và cập nhật giá trị trường của người hoặc nhóm.
SharePoint Online - Nhận Giá trị Trường Bộ chọn Người bằng PowerShell:
Để lấy giá trị trường người hoặc nhóm trong SharePoint Online, hãy sử dụng PowerShell này.
#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/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="5"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get the People picker Column of the list item
$FieldValue = [Microsoft.SharePoint.Client.FieldUserValue]$ListItem[$FieldName]
#Get the Display Name and Email Field
Write-host $FieldValue.LookupValue
Write-host $FieldValue.Email
PowerShell để truy xuất các giá trị trường người hoặc nhóm nhiều giá trị trong SharePoint Online:
#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/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get the Column Values
$FieldValues = [Microsoft.SharePoint.Client.FieldUserValue[]]$ListItem[$FieldName]
Write-host -f Yellow "Found $($FieldValues.Count) Users in the Given Field Value!"
#Get Each User from the collection
ForEach($FieldValue in $FieldValues)
{
#Get the Display Name and Email Field
Write-Host -f Green $FieldValue.LookupValue : $FieldValue.Email
}
Bạn có thể lấy đối tượng SPUser từ giá trị Cột Người hoặc Nhóm dưới dạng:
#Get the "Created By" column value of the Item
$CreatedBy = $ListItem.FieldValues["Author"]
#Get the User Object
$User = $Ctx.Web.GetUserById($CreatedBy.LookupId)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
$User | Select Email, Title, LoginName
Cập nhật Giá trị trường Người hoặc Nhóm bằng PowerShell:
Đây là PowerShell để cập nhật cột người hoặc nhóm trong SharePoint Online.
#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/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="25"
$UserID="salaudeen.rajack@crescent.com"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get the User to Update
$SPOUser = $Ctx.Web.EnsureUser($UserID)
$Ctx.Load($SPOUser)
$Ctx.ExecuteQuery()
If($SPOUser -ne $Null)
{
#Update the People picker column
$ListItem[$FieldName] = $SPOUser
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-Host -f Green "List Item has been updated!"
}
PowerShell để đặt giá trị trường của bộ chọn người có nhiều giá trị trong SharePoint Online
Để đặt trường người hoặc nhóm cho phép nhiều giá trị, hãy sử dụng PowerShell này:
#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/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"
$UserIDs=@("salaudeen.rajack@crescent.com","Charles.jonathan@crescent.com","mark.jones@crescent.com")
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Resolve each user ID and get the User Objects
$UserValueColl = @()
ForEach($UserID in $UserIDs)
{
#Get the User to Update
$SPOUser = $Ctx.Web.EnsureUser($UserID)
$Ctx.Load($SPOUser)
$Ctx.ExecuteQuery()
If($SPOUser -ne $Null)
{
$SPOUserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
$SPOUserValue.LookupId = $SPOUser.id
$UserValueColl += $SPOUserValue
}
}
IF($UserValueColl.length -gt 0)
{
$UserValueCollCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$UserValueColl
#Update the Multi-People picker column
$ListItem[$FieldName] = $UserValueCollCollection
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-Host -f Green "List Item has been updated!"
}
Cập nhật giá trị trường của Người hoặc Nhóm bằng PnP PowerShell
Các trường Người hoặc Nhóm có thể được cập nhật bằng ID Email của người dùng hoặc “id” của người dùng hoặc nhóm trong Danh sách Thông tin Người dùng Trang web. Sử dụng định dạng này để đặt giá trị trường của bộ chọn người: @{“PersonField”=“user@domain.com”,”1″}, Ở đây, “1” là ID của người đó.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$FieldName = "Project_x0020_Manager"
$ItemID = 2
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Update person or group field value using PnP PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = "Salaudeen@crescent.com"}
Tương tự, để cập nhật cột nhiều người hoặc nhóm bằng ID người dùng, hãy sử dụng cách sau:
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = 13, 23}
Ở đây, “13” và “23” là Id của người dùng trong tuyển tập trang SharePoint Online. Bài đăng khác của tôi để truy xuất và cập nhật các giá trị trường Bộ chọn người cho SharePoint Tại chỗ bằng cách sử dụng PowerShell: PowerShell để Nhận hoặc Đặt Giá trị Trường Người hoặc Nhóm (Bộ chọn Người) trong SharePoint
