How to add SharePoint list items using SharePoint Online Management Shell?

33 Views Asked by At

I tried to add the SharePoint list items using PnP PowerShell using the "Add-PnPListItem" command.

I need to add the SharePoint list items using SharePoint Online Management Shell.

Suggest SharePoint Online Management Shell command to add sharepoint list items....

1

There are 1 best solutions below

0
Karley Zhou-MSFT On

Please try this SharePoint Online PowerShell:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Config Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Tasks"
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
  
    #sharepoint online powershell add list item
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
    $ListItem = $List.AddItem($ListItemInfo)
     
    #Set Column Values
    $ListItem["Title"] = "Project Darwin"
 
    #Set People Picker Field value
    $AssignedToUser = $Ctx.web.EnsureUser("[email protected]")
    $ListItem["AssignedTo"] = $AssignedToUser
     
    #Set Date Fields
    $ListItem["StartDate"] = "01/07/2015"
    $ListItem["DueDate"] = "01/08/2015"
     
    #Set Percentage Field
    $ListItem["PercentComplete"] = "0.2" #20%
 
    #add item to sharepoint online list powershell
    $ListItem.Update()
    $Ctx.ExecuteQuery()
  
    Write-host -f Green "New Item has been added to the List!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
} 

Reference:SharePoint Online: Add New List Item using PowerShell