Tìm kiếm trang web

Thêm-Xóa người dùng vào Trường bộ chọn người có nhiều giá trị của SharePoint bằng PowerShell


Yêu cầu:
Chúng tôi có danh sách SharePoint có tên là “Dự án” với hàng nghìn mục. Danh sách này có một trường tên là “Thành viên nhóm” cho phép nhiều giá trị người dùng. Chúng tôi thường nhận được yêu cầu thêm hoặc xóa một người dùng cụ thể khỏi tất cả các mục hoặc các mục cụ thể được lọc theo các cột khác trong danh sách.

Giải pháp:

Có thể thực hiện việc thêm-xóa người dùng khỏi nhiều mục hàng loạt bằng PowerShell. Hãy sử dụng PowerShell để thêm hoặc xóa người dùng khỏi giá trị trường bộ chọn người (người hoặc nhóm) SharePoint.

PowerShell để thêm người dùng mới vào trường Người hoặc Nhóm:


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!"     
}

PowerShell để xóa người dùng khỏi trường Bộ chọn người nhiều người dùng:


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!"     
 }

Bài viết liên quan