#powershell #sharepoint-online
#powershell #sharepoint-online
Вопрос:
Я хочу добавить нескольких пользователей в поле person с несколькими пользователями в списке SharePoint. Я пробовал приведенный ниже код, но всегда получал ошибку, которая:
Новый объект: не удается найти тип [Microsoft.SharePoint.SPFieldUserValueCollection]: убедитесь, что загружена сборка, содержащая этот тип.
Кто-нибудь может помочь мне решить эту проблему?
$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="domainarzoo; domainashwin"
$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()
Ответ №1:
Для этого можно легко использовать PnP Powershell.
#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}