Return list values from column type 'User' and replace that user if they exist using PS in SP2016?
-
20-02-2021 - |
Domanda
First off my code is this.
foreach ($list in $web.lists)
{
foreach ($field in $list.fields)
{
if ($field.type -eq 'User')
{
write-host $list.Title
write-host $field
write-host ""
}
}
}
I can get all the lists for the web, check each lists fields and if the field is a 'User' field (People Picker Value); I can get the list title and the 'User' type field name.
This works fine to find the lists I want to query.
Now I want to query those list fields for a particular user and replace that user with another user.
I can't seem to figure out how to pull back the actual data that is in the column.
Nessuna soluzione corretta
Altri suggerimenti
Follow the example bellow, this reuse your script above:
function queryAndUpdateUser($siteUrl, $oldUser, $newUser){
$web = Get-SPWeb -Identity $siteUrl
$userToChange = $web.EnsureUser($oldUser)
$newestUser = $web.EnsureUser($newUser)
foreach ($list in $web.lists)
{
foreach ($field in $list.fields)
{
if ($field.type -eq 'User')
{
foreach ($item in $list.Items){
if($item[$field.InternalName] == $userToChange){
$item[$field.InternalName] = $newestUser
$item.Update()
}
}
}
}
}
}
queryAndUpdateUser -siteUrl "http://spsiteurl" -oldUser "DOMAIN\oldUser" -newUser "DOMAIN\newUser"
I hope I helped and good luck in your challenge!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange