Tìm kiếm trang web

Nhận giá trị trường của Người hoặc Nhóm (Bộ chọn Người) bằng PowerShell trong SharePoint


Yêu cầu: SharePoint PowerShell để nhận và đặt giá trị trường của người hoặc nhóm

Cột Người hoặc Nhóm trong SharePoint ghi lại giá trị của người dùng. Đây là bộ sưu tập tập lệnh PowerShell của tôi để hoạt động với các giá trị trường của bộ chọn người:

Đọc Giá trị trường của Bộ chọn người bằng PowerShell:

Đây là tập lệnh PowerShell để lấy giá trị của trường Người hoặc Nhóm.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "https://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Iterate through each row in the list
foreach ($Item in $list.Items)
{
    #Get the People picker field value
    $AssignedTo = New-Object Microsoft.Sharepoint.SPFieldUserValue($Web,$Item[$FieldName])

    #Get the Login Name of the user
    Write-host $AssignedTo.User.LoginName
    #Get the Email
    Write-host $AssignedTo.User.Email
}

Nhận Giá trị Trường Bộ chọn Người có Nhiều giá trị (Nếu bật Cho phép nhiều lựa chọn) bằng PowerShell:

SharePoint PowerShell để lấy trường người hoặc nhóm.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "https://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Iterate through each row in the list
foreach ($Item in $list.Items)
{
    if($item[$FieldName])
    {
        #Get People picker field values collection
        $UserCollection = New-Object Microsoft.Sharepoint.SPFieldUserValueCollection($Web,$item[$FieldName].ToString())
        Write-host Item ID: $item["ID"]
        
        #Get each User from the Person or Group field
        foreach($User in $UserCollection)
        {
             #Get the Display Name of the user
             Write-host $User.LookupValue 
             #Get the Login Name (Account ID)
             Write-host $User.User
             #Get the E-mail
             Write-host $User.User.Email             
        }
    }
}

Tập lệnh PowerShell để cập nhật trường cá nhân hoặc nhóm

Hãy đặt trường người hoặc nhóm bằng PowerShell trong SharePoint:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "https://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Get the List Item to update
$ListItem  = $List.GetItemByID(2)

#User Account to set
$UserAccount="Crescent\Salaudeen"
#To Add new List Item, use $Item = $List.AddItem()
$User = Get-SPUser -Identity $UserAccount -Web $web
$ListItem[$FieldName] = $User
$ListItem.Update()

Cập nhật nhiều giá trị Trường bộ chọn người bằng tập lệnh PowerShell


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Variables
$SiteURL = "https://portal.crescent.com/sites/Sales"
$ListName = "Tasks"                
$FieldName="Assigned To"
 
#Get site and List objects
$web = Get-SPWeb $SiteURL
$list= $web.lists[$listName]

#Get the List Item to update
$ListItem  = $List.GetItemByID(2)

#User Account to set
$UserAccounts="Crescent\Salaudeen; Crescent\Ravi"
$UserAccountsColl = $UserAccounts -split ';'

$UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($UserAccount in $UserAccountsColl)
{
    #Get the User
    $User=$web.EnsureUser($UserAccount)
    
    #Add to collection
    $UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
    $UserCollection.Add($UserFieldValue)
}

#update the Multiple value Person or Group field
$ListItem[$FieldName] = $UserCollection
$ListItem.Update()

Bài viết liên quan