PowerShell kullanarak SharePoint Çok Değerli Kişi Seçici Alanına Kullanıcı Ekle-Kaldır


Gereksinim:
1000'lerce öğeden oluşan "Projeler" adında bir SharePoint listemiz var. Listede birden fazla kullanıcı değerine izin veren "Ekip Üyeleri" adında bir alan bulunur. Çoğu zaman, belirli bir kullanıcıyı listedeki diğer sütunlar tarafından filtrelenen tüm öğelere veya belirli öğelere ekleme veya kaldırma zorunluluğuyla karşılaşırız.

Çözüm:

Birden fazla öğeden toplu olarak kullanıcı ekleme-çıkarma işlemleri PowerShell ile gerçekleştirilebilir. SharePoint kişi seçici (kişi veya grup) alan değerine kullanıcı eklemek veya bu alandan kullanıcı kaldırmak için PowerShell'i kullanalım.

Kişi veya Grup Alanına yeni kullanıcı eklemek için PowerShell:


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

Kullanıcıyı Çok Kullanıcılı Kişi Seçici alanından kaldırmak için PowerShell:


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

İlgili makaleler