Unable to add list item using Powershell for our online site collection

john john 1,021 Reputation points
2022-03-24T22:59:42.667+00:00

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

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CaseyYang-MSFT 10,461 Reputation points
    2022-03-25T10:09:25.187+00:00

    Hi @john john

    Do you want to import CSV file into SharePoint list using PowerShell?

    You could use following PowerShell commands as a workaround:

    #Load SharePoint CSOM Assemblies  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
        
    ##Variables for Processing  
    $SiteUrl = "https://xxx.sharepoint.com/sites/xxx/"  
    $ListName="xxx"  
    $ImportFile ="c:\xxx\xxx.csv"  
    $UserName="******@xxx.com"  
    $Password ="xxxxxx"  
       
    #Setup Credentials to connect  
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
       
    #Set up the context  
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
    $Context.Credentials = $credentials  
        
    #Get the List  
    $List = $Context.web.Lists.GetByTitle($ListName)  
       
    #Get the Data from CSV and Add to SharePoint List  
    $data = Import-Csv $ImportFile  
    Foreach ($row in $data) {  
        #add item to List  
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation  
        $Item = $List.AddItem($ListItemInfo)  
        $Item["xxx"] = $row.xxx  
        $Item.Update()  
        $Context.ExecuteQuery()  
          
    }  
    Write-host "CSV data Imported to SharePoint List Successfully!"  
    

    For Reference:

    SharePoint Online: Import CSV File into SharePoint List using PowerShell
    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.