John John
John John

Reputation: 1

Unable to create SharePoint online list items using Power shell

I want to add list items inside a SharePoint online list, so i run this command:-

 $SiteUrl = "https://***.sharepoint.com/sites/t"
 $ListName= "Child2"
 Connect-PnPOnline -Url $SiteUrl -UseWebLogin
 $Ctx = Get-PnPContext
    
 #Get the list Item
 $List=$Ctx.Web.Lists.GetByTitle($ListName)
    
 $Import = Import-Csv -Path "C:\CSV\finaldelta3.csv"
    
 for ($counter=0; $counter -lt $Import.Length; $counter++){
    
 $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
 $ListItem = $List.AddItem($ListItemInfo)
    
 #Set Column Values
 $ListItem["Title"] = "Hello World!"
    
 #Apply changes to list
 $ListItem.Update()
 $Ctx.ExecuteQuery()
 }

now on one tenant, i will not get any error, but the list item will not get created, while on another tenant i got this exception and also the item will not get created as well:-

Cannot convert argument "parameters", with value: "Microsoft.SharePoint.Client.ListItemCreationInformation", for "AddItem" to type "Microsoft.SharePoint.Client.ListItemCreationInformation": "Cannot convert the "Microsoft.SharePoint.Client.ListItemCreationInformation" value of type "Microsoft.SharePoint.Client.ListItemCreationInformation" to type "Microsoft.SharePoint.Client.ListItemCreationInformation"." At line:4 char:1 + $ListItem = $List.AddItem($ListItemInfo) +

     + CategoryInfo          : NotSpecified: (:) [], MethodException
     + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
      Cannot index into a null array.  At line:7 char:1  + $ListItem["Title"] = "Hello World!"#$Import[$counter].'Caller Info' #
...  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId : NullArray
      You cannot call a method on a null-valued expression.  At line:10 char:1  + $ListItem.Update()  + ~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId : InvokeMethodOnNull

so any idea what is going on? Thanks

Upvotes: 0

Views: 691

Answers (1)

Minus50DKP
Minus50DKP

Reputation: 11

Have you tried updating your SPO Management Shell: https://www.microsoft.com/en-in/download/details.aspx?id=35588

And/or the SPO Client Component SDK: https://www.microsoft.com/en-us/download/details.aspx?id=42038

I'm not sure if it is required by PnPOnline, but with SPOServices you need to load the assembly as well:

    Add-Type -Path "$Env:ProgramFiles\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "$Env:ProgramFiles\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    Add-Type -Path "$Env:ProgramFiles\SharePoint Client Components\16.0\Assemblies\Microsoft.Online.SharePoint.Client.Tenant.dll" 

Or alternatively:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Online.SharePoint.Client.Tenant") | Out-Null

Personally, I start my script with

#Requires -Module Microsoft.Online.SharePoint.PowerShell

Upvotes: 1

Related Questions