当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在 SharePoint 多值人员选取器字段中添加或删除用户

作者:精品下载站 日期:2024-12-14 14:21:18 浏览:15 分类:玩电脑

使用 PowerShell 在 SharePoint 多值人员选取器字段中添加或删除用户


要求:
我们有一个名为“项目”的 SharePoint 列表,其中包含 1000 个项目。该列表有一个名为“团队成员”的字段,它允许多个用户值。我们经常需要向所有项目或列表中其他列过滤的特定项目添加或删除特定用户。

解决方案:

可以使用 PowerShell 来批量添加或删除多个项目中的用户。让我们使用 PowerShell 在 SharePoint 人员选取器(人员或组)字段值中添加或删除用户。

[玩转系统] 使用 PowerShell 在 SharePoint 多值人员选取器字段中添加或删除用户

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

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

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯