Add-PnpListItem does not bubble up exceptions
Reporting an Issue or Missing Feature
Add-PnpListItem reports an error in the output such as "Invalid Text Value", but if I put a try/catch around Add-PnpListItem, it does not go into the catch block.
Expected behavior
The error should be thrown to me so I can catch it.
Actual behavior
I see error output, but cannot catch it.
Steps to reproduce behavior
Add a list item with more than 255 characters to a single line text field. Put a try/catch around Add-PnpListItem. The error is shown to the user, but does not go into the catch block.
Which version of the PnP-PowerShell Cmdlets are you using?
- [ ] PnP PowerShell for SharePoint 2013
- [ ] PnP PowerShell for SharePoint 2016
- [x] PnP PowerShell for SharePoint Online
What is the version of the Cmdlet module you are running?
2.17.17...
How did you install the PnP-PowerShell Cmdlets?
- [ ] MSI Installed downloaded from GitHub
- [x] Installed through the PowerShell Gallery with Install-Module
- [ ] Other means
I experience the same problem with "Apply-PnPProvisioningTemplate"
This is a workaround i found in another thread. Save the result as a variable (and suppress the error) and check if the variable is null. It doesn't throw an exception error when you see the message hence why the catch doesn't work and this is by design so your scripts can carry on running. Hope this helps, it resolved it for me. Thank you @erwinvanhunen
$termgroupcheck = Get-PnPTermGroup -Identity "$termgroup" -ErrorAction SilentlyContinue
if($termgroupcheck -eq $null) { Write-Host "$termgroupcheck doesn't exist" } else { Write-Host "$termgroupcheck does exist" }
You could use the Get-PnPException cmdlet for this. https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpexception?view=sharepoint-ps
We have the same issue with Set-PnPListItem.