locked
Help in updating multiple person field via powershell - Only updates one user RRS feed

  • Question

  • Im trying to update multiple person field but it only updates one person not multiple users.ONLY THE LAST ONE

    As you can see below i'm picking up display name from a field "Contract Viewer" which has values like so: "abc,def,ghi"

    Sure i'm missing something.
    Thanks in Advance


    See code below:
            
        function Update([string] $itemid)
           {
           [Microsoft.SharePoint.SPFieldUserValueCollection] $ReviewersList = New-Object Microsoft.SharePoint.SPFieldUserValueCollection
           $CMRSItems = $list.Items | where  {$_['ID'] -eq $itemid}
           $CMRSItems | ForEach-Object {
           
           #my real values are like this "abc,def,ghi"
           
           $realval =  $_['Contract Viewer']
           #$split = $realval.Split(";#").Trim()
            
            $array = @($realval.Split(','))
                for ($i = 0; $i -lt $array.Count - 1; $i++)
                {
                    $number = $array[$i]
                    
                    $user = getSPuser -webURL $webURL -displayname $number
                    #Check if user exists
                    if($user -ne $null)
                    {
                    #Get list from Ensurer
                    $userlist = GetReviewLists -user $user
                    Write-Host $userlist
                    #add userslist
                    $ReviewersList.Add($userlist)
                    }            
                    
                }
                
                 Write-Host $ReviewersList
                 #Not sure its aoonly updating the last one
                 $CMRSItems["Contract Viewer1"] = $ReviewersList;            
                 $CMRSItems.Update();
                 $web.Dispose();
                
            }       
                    
        }
        
        
         function getSPuser([string] $webURL,[string] $displayname)
        {
            $user=Get-SPUser –web $webURL | where{$_.displayname -contains $displayname}
            $email = $user.email
            return $email
        }
        
        function GetAllItems()
           {
            #this loops through items from 1 to 2 for example
            $CMRSItems = $list.Items | where  {($_['ID'] -gt 0) -and ($_['ID'] -le 2)}
            $CMRSItems | ForEach-Object {
            Write-Host "RefID: " $_['RefID']
            Write-Host "ItemID: " $_['ID']
            Update -itemid $_['ID']
            }
        }
        
        
        function GetReviewLists([string] $userlist)
            {
            
            $spUser = $web.EnsureUser($userlist)
            $ReviewerValue = new-object Microsoft.SharePoint.SPFieldUserValue($web, $spUser.ID, $spUser.LoginName)
            return $ReviewerValue              
            
            
            }
        GetAllItems
    
    `

    Friday, April 11, 2014 7:48 AM

All replies

  • Hi Patrick,

    Did you get the solution for this problem?

    Actually I am also trying to populate multi user field in SharePoint list through Powershell. It only updates the last users name or any random user. The output of the powershell script is correct and also in correct format.

    But when I update the list item, I get some randome values which is totally incorrect.

    If you have solve the problem, please let me know the solution.

    Thanks

    Friday, June 5, 2020 4:13 PM