PowerShell, Çok Değerli Kişi Seçici Alanını Yeni Kullanıcılarla Güncelleyecek


Gereksinim: SharePoint'te PowerShell'i kullanarak mevcut Kişi Seçici alanı değerini güncelleyin.

Mevcut Kişi Seçici alanına yeni bir kullanıcı eklemek için PowerShell:

Projeler listesinde “Ekip Üyeleri” adında bir alanımız var ve belirli bir liste öğesi için bu alana yeni üye(ler) eklemek istedik.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "https://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 Kullanarak Kişi Seçici Alanı Nasıl Ayarlanır?

Yukarıdaki komut dosyası, mevcut kişi seçici alanı değerini yeni kullanıcılarla günceller ve bu, kişi seçici alanını, içindeki mevcut değerleri güncellemeden ayarlar.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#parameters
$SiteURL = "https://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!"

İlgili makaleler