Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Add-Remove User to SharePoint Multi-Valued People Picker Field using PowerShell

$
0
0
Requirement: 
We've a SharePoint list called "Projects" with 1000's of items. The list has a field called "Team Members" which allows multiple user values. We often get a requirement to either add or remove a particular user to all items or specific items filtered by other columns in the list.

Solution: Adding-removing user from multiple items in a bulk can be achieved with PowerShell. Lets use PowerShell to add or remove user from SharePoint people picker (person or group) field value.

PowerShell to Add new user to Person or Group Field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$SiteURL = "http://portal.crescent.com/projects/"
$ListName = "Projects"
$FieldName= "TeamMembers"
$UserToAdd="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Item Ids to update
$IDColl= @(4,5,7,14,55,169,258,260,261)

#Iterate through each item
foreach($ItemID in $IDColl)
{
#Get the Item
$Item = $List.GetItembyID($ItemID)
Write-Host "Processing: "$item["ProjectName"]

#Get Existing field value
$MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]

#Prepre the user to Add
$User = $Web.EnsureUser($UserToAdd)

#Add new user to the collection
$NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID,$User.Name)
$MultiUserCollection.Add($NewUser)

#Update the field value
$item[$FieldName] = $MultiUserCollection
$item.update()

write-host "Team Member Added!"
}

PowerShell to remove user from Multi-user People Picker field:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$SiteURL = "http://portal.crescent.com/projects.abraaj.com/projects/"
$ListName = "Projects"
$FieldName= "TeamMembers"
$UserToRemove="Crescent\Sam"

#Get site and List objects
$web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

$IDColl= @(184,259,281,282,306,318,331,378,404,410)
foreach($ItemID in $IDColl)
{
#Get the Item
$Item = $List.GetItembyID($ItemID)
Write-Host "Processing: "$item["ProjectName"]

#Get Existing field value
$MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
$NewUserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection

#Prepre the user to remove
$User = $Web.EnsureUser($UserToRemove)

#Create a new collection - exclude user to remove
Foreach($MultiUser in $MultiUserCollection)
{
if($MultiUser.User.LoginName -ne $User.LoginName)
{
#Add user to new collection
$NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $MultiUser.User.ID,$MultiUser.User.Name)
$NewUserCollection.Add($NewUser)
}
}
#Update the list item
$item[$FieldName] = $NewUserCollection
$item.update()

write-host "User Removed From Existing People Picker field Value!"
}

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>