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

[玩转系统] PowerShell 将使用新用户更新多值人员选择器字段

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

PowerShell 将使用新用户更新多值人员选择器字段


要求:使用 SharePoint 中的 PowerShell 更新现有人员选取器字段值。

PowerShell 将新用户添加到现有人员选择器字段:

我们在项目列表中有一个名为“团队成员”的字段,并且希望向其中添加特定列表项的新成员。


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 将使用新用户更新多值人员选择器字段

如何使用 PowerShell 设置人员选择器字段

上面的脚本使用新用户更新现有人员选择器字段值,而此脚本设置人员选择器字段而不更新其中的现有值。


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

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

取消回复欢迎 发表评论:

关灯