Búsqueda de sitios web

Obtener valor de campo de persona o grupo (selector de personas) usando PowerShell en SharePoint


Requisito: SharePoint PowerShell para obtener y establecer el valor del campo de persona o grupo

La columna Persona o Grupo en SharePoint captura los valores del usuario. Aquí está mi colección de scripts de PowerShell para trabajar con valores de campo del selector de personas:

Leer el valor del campo del selector de personas usando PowerShell:

Aquí está el script de PowerShell para obtener el valor de un campo de Persona o Grupo.


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
}

Obtenga el valor del campo del selector de personas con valores múltiples (si está habilitado Permitir selecciones múltiples) usando PowerShell:

SharePoint PowerShell para obtener el campo de persona o grupo.


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             
        }
    }
}

Script de PowerShell para actualizar el campo de persona o grupo

Configuremos el campo de persona o grupo usando PowerShell en 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()

Actualizar el campo Selector de personas de valores múltiples mediante el script de 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()

Artículos relacionados