首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在多用户人员选择器字段(人员或组字段)中添加多个用户

如何在多用户人员选择器字段(人员或组字段)中添加多个用户
EN

Stack Overflow用户
提问于 2019-04-11 22:07:30
回答 1查看 1.9K关注 0票数 0

我想在SharePoint列表的多选取器person字段中添加多个用户。我尝试了下面的代码,但总是得到一个错误:

Microsoft.SharePoint.SPFieldUserValueCollection:

新对象:找不到类型,请验证是否加载了包含此类型的程序集。

有人能帮我解决这个问题吗?

代码语言:javascript
复制
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Securedpw = ConvertTo-SecureString $Password -AsPlainText -Force
$ClientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Securedpw)
$web = $ClientContext.Web 
$list= $web.lists.GetByTitle($listName)
$ClientContext.Load($list)
$ClientContext.ExecuteQuery()
$ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem= $list.AddItem($ListItemCreationInformation)

$UserAccounts="domain\arzoo; domain\ashwin"
$UserAccountsColl = $UserAccounts -split ';'
$UserCollection = new-object Microsoft.SharePoint.SPFieldUserValueCollection
foreach($UserAccount in $UserAccountsColl)
{    
   #Get the User
    $User=$web.EnsureUser($UserAccount)

    #Add to collection
    $UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
    $UserCollection.Add($UserFieldValue)
}

#update the Multiple value Person or Group field
$ListItem[$FieldName] = $UserCollection
$ListItem.Update()
EN

回答 1

Stack Overflow用户

发布于 2019-04-12 09:13:26

您可以很容易地使用PnP Powershell来实现这一点。

代码语言:javascript
复制
#region Variables 
$Username = "lee@tenant.onmicrosoft.com" 
$Password = "pw" 
$siteURL = "https://tenant.sharepoint.com/sites/lee" 

#endregion Variables

#region Credentials 
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass) 
#endregion Credentials

Connect-PnPOnline -Url $siteURL -Credentials $PSCredentials
$users = "user1@tenant.onmicrosoft.com", "user2@tenant.onmicrosoft.com"
Set-PnPListItem -List "MyList" -Identity 9 -Values @{"User"=$users}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55634506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档