Sunday, December 27, 2015

PowerShell to Update Multi-valued Peole Picker Field with New Users

Requirement: Update existing People Picker field value using PowerShell in SharePoint.

PowerShell script to Add an new user to existing People Picker field:
We have a field called "Team Members" in Projects list and wanted to add new member(s) to it for a particular list item.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "http://portal.crescent.com/projects/"
$ListName = "Projects"                
$FieldName= "ProjectTeamMembers"
$UserToAdd="Crescent\Faris"
$ItemID=431

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

if($List -ne $null)
    {           
        #Get the Item
        $Item = $List.GetItembyID($ItemID)
        
        #Get Existing field value
        $MultiUserCollection = [Microsoft.SharePoint.SPFieldUserValueCollection]$item[$FieldName]
        
        #Prepre the user to add
        $User = $Web.EnsureUser($UserToAdd)
        $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
        
        #Update Multivalued people Picker field 
        $MultiUserCollection.Add($NewUser)
        $item[$FieldName] = $MultiUserCollection
        $item.update()       
    }
write-host "New User Added and Existing People Picker field value updated!"     
powershell to update people picker in sharepoint

How to Set the People Picker Field using PowerShell:
The above script updates existing people picker field value with new users, this one sets people picker field without updating existing values in it.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#parameters
$SiteURL = "http://portal.crescent.com/projectpipeline/"
$ListName = "Projects"                
$FieldName= "ProjectMembers"
$TeamMembers=@("Crescent\Markb", "Crescent\Andrew")

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

    #Get the Item
    $Item = $List.GetItembyID(1)
    Write-Host "Processing: "$item["ProjectName"]
    
    #Object Array for People picker value
    $TeamMembersCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
    
  #Add Each Team member to the array 
  foreach ($TeamMember in $TeamMembers)
  {      
 #Prepre the user to Add
 $User = $Web.EnsureUser($TeamMember)

 #Add new user to the collection
 $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID,$User.Name)
 $TeamMembersCollection.Add($NewUser)
   }
 #Update the field value
 $item[$FieldName] = $TeamMembersCollection
 $item.update()
 
write-host "Team Member Field updated!"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...