powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Add-PnPListItem & Batching doesn't work with a French created site collection & credential connection

Open hervedorierai3 opened this issue 3 years ago • 8 comments

Reporting an Issue or Missing Feature

I have to import data from CSV file to a list. I use Add-PnPListItem with -Batch option to improve the import time.

I developped my script in a English site collection : no problem at all. I tested my script in a French site collection : the Add-PnPListItem fails with error "Object reference not set to an instance of an object." If I remove the -Batch option, the items are created in the list (but it's very, very, very long). If I connect to the site collection with Connect-PnpOnline -Interactive, the items are created in the list (but it's a batch, so I can't connect with this option).

I'm sure that it's not a permission issue because the script delete all item in the list (Remove-PnPListItem -Batch). I'm connected with an Azure AD Application (Register-PnPAzureADApp -ApplicationName "MyBatchApp" -Tenant xxxx.onmicrosoft.com -SharePointApplicationPermissions Sites.Manage.All -Password (ConvertTo-SecureString -String "xxxxxxx" -AsPlainText -Force) -DeviceLogin )

Expected behavior

Would expect the item is successfully added to the list 😊

Actual behavior

Add-PnPListItem fails with error "Object reference not set to an instance of an object."

image

Steps to reproduce behavior

  • Create a "Communication" site collection with "French" as language.
  • Create a simple list (only the Title column) named "BatchTest"
  • Connect to the site collection with Certificate (Connect-PnPOnline -Url $siteurl -ClientId $application_clientid -Tenant $tenant_name -Thumbprint $certificate_thumbprint)
  • Try to add an item to the list with Add-PnPListItem and -Batch option ==> Fails 😨 $batch_add = New-PnPBatch Add-PnPListItem -List "BatchTest" -Value @{'Title'= 'Add with batch option'} -ContentType "Item" -Batch $batch_add Invoke-PnPBatch -Batch $batch_add
  • Try to add an item to the list with Add-PnPListItem but without -Batch option ==> Success 😊 Add-PnPListItem -List "BatchTest" -Value @{'Title'= 'Add without batch option'} -ContentType "Item"

What is the version of the Cmdlet module you are running?

2.2.55 nigthly

Which operating system/environment are you running PnP PowerShell on?

  • [x] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

hervedorierai3 avatar Aug 25 '23 15:08 hervedorierai3

@hervedorierai3 , please try to provide minimal steps to reproduce this issue.

Would suggest that you try it by adding a single list item via batch and also via CSOM and let us know.

gautamdsheth avatar Sep 04 '23 09:09 gautamdsheth

@gautamdsheth : Added sample code. Added mention that with interactive connection there is no problem.

hervedorierai3 avatar Sep 04 '23 19:09 hervedorierai3

I have a similar issue when running New-PnPSite. If I use the ambiant connection, no issue. But If I use a returned connection, it fails with the exact same error message.

Working:

Import-Module PnP.Powershell

$tenant = "myCorporate"
$sitePrefix = "Testing"
$timestamp = (Get-Date -Format s) -replace "[\:|T]", "-"
$siteName = $sitePrefix + " " + $timestamp
$alias = $siteName.Replace(" ", "-")
$siteUrl = "https://$tenant.sharepoint.com/sites/$alias"
Connect-PnPOnline -Url "https://$tenant.sharepoint.com" -Interactive 

$myLogin = Get-PnPProperty -ClientObject (Get-PnPContext).Web.CurrentUser -Property Loginname 

# & "$PSScriptRoot\Run-DeploySiteScriptsAndSiteDesigns.ps1"

New-PnPSite -Type TeamSite `
    -Title $siteName  `
    -Alias $alias  `
    -Wait  `
    -Lcid 1036 `
    -Owner $myLogin 

Non working:

Import-Module PnP.Powershell

$tenant = "myCorporate"
$sitePrefix = "Testing"
$timestamp = (Get-Date -Format s) -replace "[\:|T]", "-"
$siteName = $sitePrefix + " " + $timestamp
$alias = $siteName.Replace(" ", "-")
$siteUrl = "https://$tenant.sharepoint.com/sites/$alias"
if (-not $adminCnx) {
    $adminCnx = Connect-PnPOnline -Url "https://$tenant.sharepoint.com" -Interactive -ReturnConnection
}

$myLogin = Get-PnPProperty -ClientObject $admincnx.Context.Web.CurrentUser -Property Loginname -Connection $admincnx

# & "$PSScriptRoot\Run-DeploySiteScriptsAndSiteDesigns.ps1"

New-PnPSite -Type TeamSite `
    -Title $siteName  `
    -Alias $alias  `
    -Wait  `
    -Lcid 1036 `
    -Owner $myLogin  `
    -Connection $adminCnx

Strangely, if I have an ambient connection, everything is OK even with passing connection parameter. I guess there's somewhere in the pnp powershell code a reference to the ambient connection instead of the connection from parameters.

If it can help, the stack trace (grabbed using $Error[0].Exception.toString()) is:

System.Management.Automation.PSInvalidOperationException: File Not Found.
   at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in c:\build\src\Commands\Base\PnPConnectedCmdlet.cs:line 101
   at System.Management.Automation.CommandProcessor.ProcessRecord()

Which correspond to this error shallowing code: https://github.com/pnp/powershell/blob/b6c927e6d24d796e32377f5e232c73c12eeec5d6/src/Commands/Base/PnPConnectedCmdlet.cs#L99-L102

stevebeauge avatar Sep 07 '23 09:09 stevebeauge

Hello ! Nobody reproduces this issue ?

hervedorierai3 avatar Oct 04 '23 13:10 hervedorierai3

@gautamdsheth could you remove the 'needmoreinformation' tag please ?

hervedorierai3 avatar Oct 24 '23 06:10 hervedorierai3

@hervedorierai3 : I understand it's a 3 year old issue already, can you still reproduce the issue with the latest build?

reshmee011 avatar Jan 21 '26 15:01 reshmee011

This repro is working as expected now:

Import-Module PnP.Powershell

$tenant = "myCorp"
$sitePrefix = "Testing"
$timestamp = (Get-Date -Format s) -replace "[\:|T]", "-"
$siteName = $sitePrefix + " " + $timestamp
$alias = $siteName.Replace(" ", "-")
$siteUrl = "https://$tenant.sharepoint.com/sites/$alias"
if (-not $adminCnx) {
    $adminCnx = Connect-PnPOnline -Url "https://$tenant.sharepoint.com" -Interactive -ReturnConnection
}

$myLogin = Get-PnPProperty -ClientObject $admincnx.Context.Web.CurrentUser -Property Loginname -Connection $admincnx

# & "$PSScriptRoot\Run-DeploySiteScriptsAndSiteDesigns.ps1"

New-PnPSite -Type TeamSite `
    -Title $siteName  `
    -Alias $alias  `
    -Wait  `
    -Lcid 1036 `
    -Owner $myLogin.Replace("i:0#.f|membership|", "")  `
    -Connection $adminCnx

stevebeauge avatar Jan 21 '26 15:01 stevebeauge

@stevebeauge : Amazing news, probably it might either been fixed or something changed in the back end API.

reshmee011 avatar Jan 21 '26 16:01 reshmee011