Quantcast
Channel: SharePoint 2013 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 26374

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

$
0
0
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

`


Viewing all articles
Browse latest Browse all 26374

Trending Articles



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