0

I have a SharePoint online field named "HRContacts" which allow multiple selection:-

enter image description here

and using PnP Power Shell i want to create a new list item and populate this field among with other fields. i have this Power Shell script, but i got the below error:-

PS C:\WINDOWS\system32> $HRCollabs = [System.Collections.ArrayList]@()
PS C:\WINDOWS\system32> $testuser  = Get-PnPAzureADUser -Filter  { Mail eq 'test@******.com'}
PS C:\WINDOWS\system32> $HRCollabs.Add($testuser)
PS C:\WINDOWS\system32> Add-PnPListItem -List "customlist" -Values @{HRContacts= $HRCollabs; Title = '123'}
Add-PnPListItem : The specified user  could not be found.
At line:1 char:1
+ Add-PnPListItem -List "customlist" -Values @{HRContacts= $HRCollabs; Titl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Add-PnPListItem], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Lists.AddListItem

Any advice? Thanks

2 Answers 2

1

In SharePoint, User field is a lookup to a hidden user list. A lookup field stores an ID a number, not an email. In your case, it does not work because instead of numbers you are passing emails.

Example that creates a list item with multi-select user field:

Add-PnPListItem -List customlist -Values @{HRContacts= 13, 23; Title = '123'}

In order to get the correct user ID, you first need to make sure a user exists on your site.

$testuser  = Get-PnPUser | Where-Object Email -eq 'test@******.com'
If($null -eq $testuser) {       
  $testuser = New-PnPUser -LoginName 'test@******.com'
}

Full example, based on your code

$HRCollabs = @()
$testuser  = Get-PnPUser | Where-Object Email -eq 'test@******.com'
If($null -eq $testuser) {       
  $testuser = New-PnPUser -LoginName 'test@******.com'
}
$HRCollabs += $testuser.ID


Add-PnPListItem -List customlist -Values @{HRContacts= $HRCollabs; Title = '123'}

0

I was struggling to format user email ids. Thanks @Denis for the idea of adding Lookup Ids instead of emails.

Here is the complete code of reading user emails from csv and add them to SharePoint list using PnpPowershell. This might help some one.

cls
$userCred = Get-StoredCredential -Target "UserKey"
$csvFileInfo="C:\MyTemps\STREAM\GroupWork\GroupsCsv.csv" 
$ListName = "MyStrM365UserInfo"
$csvAllIsers = @(Import-Csv -Path $csvFileInfo)  
$siteURL="https://purnatest.sharepoint.com/Sites/TestComm1/" 

Connect-PnPOnline -Url $siteURL -Credentials $userCred
#Start-Sleep -Seconds 2
$CommSite = Get-PnPSite
Write-Host "======================================================"
Write-Host "Connected to $($CommSite.Url)" -ForegroundColor Cyan

$counter=0;

foreach ($csvUserEntry in $csvAllIsers) {  
Write-Host "$($counter)-Processing Group with Email : $($csvUserEntry.ContainerEmailId)" -ForegroundColor Yellow
$ItemValue = @{} 
try {
     $ItemValue.Add("Title",$csvUserEntry.GroupName)
     $ItemValue.Add("SharePointSiteURL",$csvUserEntry.SharePointLink)
     $ItemValue.Add("GroupEmail",$csvUserEntry.ContainerEmailId)
     $ownerEmailIds = $csvUserEntry.Owners.Split(";",[System.StringSplitOptions]::RemoveEmptyEntries)  
        $OwnerIds = @()
        foreach ($ownerEmail in $ownerEmailIds)
        { Write-Host "                     Processing Owner Email: $($ownerEmail)"
            if($ownerEmail.Length -ge 0)
            {        
                $testuser  = Get-PnPUser | Where-Object Email -eq $ownerEmail
                If($null -eq $testuser) {       
                  $testuser = New-PnPUser -LoginName $ownerEmail
                }
                if($testuser -ne $null)
                    {
                      $OwnerIds += $testuser.ID
                    }
                }        
        }
    Write-Host "Owner IDs : $($OwnerIds)"
    $ItemValue.Add("Owners",$OwnerIds)  
    $listItem=Add-PnPListItem -List $ListName -Values $ItemValue 
    Write-Host "$($counter)  -Added record with ID : $($listItem.ID) and Container Email: $($csvUserEntry.ContainerEmailId) " -ForegroundColor Green
    Write-Host "=================================================================="
}
catch
{
 Write-Host "Error in adding user " $csvUserEntry.ContainerEmailId -ForegroundColor Red
}
$counter++;
}
Write-Host "Counter:" $counter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.