SharePoint Online: leer/actualizar valores de campo de persona o grupo (selector de personas) mediante PowerShell
Requisito: Obtener o establecer valores de campo "Persona o grupo" en SharePoint Online mediante PowerShell.
El campo de persona o grupo proporciona una lista de personas y grupos con capacidad de búsqueda al agregar o editar un elemento. Actúa como un campo de autocompletar cuando escribe los primeros cuatro caracteres de los nombres de usuario. Aquí está mi colección de scripts de PowerShell para recuperar y actualizar valores de campos de personas o grupos.
SharePoint Online: obtenga el valor del campo del selector de personas mediante PowerShell:
Para obtener el valor del campo de persona o grupo en SharePoint Online, use este PowerShell.
#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 para recuperar valores de campo de persona o grupo de valores múltiples en 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
}
Puede obtener el objeto SPUser del valor de la columna Persona o Grupo como:
#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
Actualizar el valor del campo de persona o grupo mediante PowerShell:
Aquí está el PowerShell para actualizar una columna de persona o grupo en 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 para establecer valores de campo del selector de personas con valores múltiples en SharePoint Online
Para configurar el campo de persona o grupo que permite múltiples valores, use este PowerShell:
#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!"
}
Actualizar valores de campos de persona o grupo usando PnP PowerShell
Los campos de Persona o Grupo se pueden actualizar con la ID de correo electrónico del usuario o la "identificación" del usuario o grupo en la Lista de información del usuario del sitio. Utilice este formato para configurar los valores del campo del selector de personas: @{“PersonField”=“user@domain.com”,”1″}, aquí, “1” es el ID de la persona.
#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"}
De manera similar, para actualizar columnas de grupos o de varias personas con ID de usuario, utilice lo siguiente:
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = 13, 23}
Aquí, "13" y "23" son los identificadores de usuarios de la colección de sitios de SharePoint Online. Mi otra publicación para recuperar y actualizar los valores de los campos del selector de personas para SharePoint local usando PowerShell: PowerShell para obtener o establecer valores de campo de persona o grupo (selector de personas) en SharePoint
