SharePoint Online: Add New List Item using PowerShell

When working with SharePoint Online, had a requirement to add new list items to a list from PowerShell script. Here is my example:

SharePoint Online: PowerShell to Add List Item
Here is the SharePoint Online PowerShell to create list item
#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://crescenttech.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("Salaudeen@TheCrescentTech.com")
    $ListItem["AssignedTo"] = $AssignedToUser
    
    #Set Date Fields
    $ListItem["StartDate"] = "01/07/2015"
    $ListItem["DueDate"] = "01/08/2015"
    
    #Set Percentage Field 
    $ListItem["PercentComplete"] = "0.2" #20%

    #Apply changes to list
    $ListItem.Update()
    $Ctx.ExecuteQuery()
 
    Write-host -f Green "New Item has been added to the List!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}  
This inserts new item to Tasks list. Here is the result: Add item to list in SharePoint Online using PowerShell.
sharepoint online powershell add item to list

SharePoint Online PowerShell to Create List Item
Lets add some error handling code and get the credentials at run time in the script. Here is the SharePoint Online PowerShell CSOM to add list items
#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"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Random"
$ItemsToCreate="1000"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
        
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #add item to list sharepoint online powershell - with Random Data
    For($i=1; $i -le $ItemsToCreate; $i++)
    {
        #Create list item
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $I
        $ListItem.Update()
        $Ctx.ExecuteQuery()

        Write-host "New Item $I Added to the List!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message
} 

PnP PowerShell to Add List Item in SharePoint Online
Here is how to add item to list in SharePoint Online with PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName ="Projects"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Add List Item - Internal Names of the columns : Value
Add-PnPListItem -List $ListName -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}
To add list items with field types such as Managed metadata, Hyperlink, etc. refer: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnplistitem?view=sharepoint-ps
SharePoint Online: Add New List Item using PowerShell SharePoint Online: Add New List Item using PowerShell Reviewed by Salaudeen Rajack on August 01, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.