SharePoint Online: Add New List Item using PowerShell
Requirement: When working with SharePoint Online, I had a requirement to add new list items to a list from the PowerShell script. This blog post will cover how to create a list item in SharePoint Online using PowerShell, with some examples.
How to add an item to a List in SharePoint Online?
Adding items to SharePoint lists is a common task that you will likely find yourself doing regularly. Here’s a quick overview of how to do it:
- Log in to your SharePoint site and navigate to the list that you want to add an item to.
- Click on the “+ New” link from the command bar.
- Enter the information for the new item in the appropriate fields.
- Click the “Save” button.
And that’s all there is to it! Lets’ see how to add items in SharePoint online list using PowerShell.
SharePoint Online: PowerShell to Add List Item
There are many situations where you may want to create a list item in SharePoint Online without going through the web-based user interface in your SharePoint Online. Luckily, we have an easy way to do this using PowerShell. In this blog post, we will show you how to add an item to a SharePoint Online list using PowerShell.
To add a list item using PowerShell, we’ve to set the value for each custom column in the list. Here is the SharePoint Online PowerShell to create a 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://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("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%
#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
}
This inserts a new item into the Tasks list. This is a quick and easy way to add an item to the list in SharePoint Online using PowerShell, without having to use the web browser user interface. Here is the result:
SharePoint Online PowerShell to Create List Item
Let’s 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 an item to list SharePoint Online PowerShell - with Random Data
For($i=1; $i -le $ItemsToCreate; $i++)
{
#create list item in sharepoint online using powershell
$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
}
This script randomly creates list items in bulk.
PnP PowerShell to Add List Item in SharePoint Online
When working with SharePoint Online, you may need to add an item to a list programmatically. Fortunately, there is an easy way to accomplish this using PowerShell. You can use PowerShell script to help automate creating content that needs to be added on a regular basis or unattended.
Here is how to add an item to the list in SharePoint Online with the PnP PowerShell cmdlet Add-PnPListItem.
- The first step is to connect to your SharePoint Online site URL. You can do this by running the following cmdlet: Connect-PnPOnline.
- Once you are connected, you can use the Add-PnPListItem cmdlet to add items to your list. For example, the following command will add a new item to a list with the field Internal name and the value for the field: Add-PnPlistItem -list “List Name” -values @{“Field Internal Name”=”Field Value”; “Field Internal Name”=”Field Value”}
Here is an example:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Add List Item - Internal Names of the columns: Value
Add-PnPListItem -List $ListName -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}
You can also specify the “ContentType” parameter with name or ID if you would like to add an item of a specific content type. Once you execute this PowerShell script, a new item will be added to the list. Adding items to SharePoint Online lists using PowerShell is a quick and easy way to populate your data.
How about Setting Fields like Managed metadata, Hyperlink, Lookup, Person or Group, etc.?
The above scripts work just fine with simple field types such as single lines of text, choice, Yes/No, etc. However, we have to handle them differently for other field types such as Managed metadata, Hyperlink, Lookup, People Picker, etc. Here are my posts to help:
- SharePoint Online: PowerShell to Update Lookup Field Value
- SharePoint Online: Update Hyperlink Field Value using PowerShell
- SharePoint Online: PowerShell to Update Lookup Field Value
- SharePoint Online: Update Managed Metadata Field Value using PowerShell
- SharePoint Online: Update Person or Group Field Values using PowerShell
- SharePoint Online: Update Choice Field Value using PowerShell
- SharePoint Online: Update “Yes/No (Check box)” Field Value using PowerShell
- SharePoint Online: PowerShell to Update Date Field Value
You can perform bulk operations such as delete, or add items to a SharePoint Online list in batches faster using the New-PnPBatch cmdlet in PnP PowerShell.
More info: How to Add Multiple List Items in Batch using PowerShell?
Although you can create a list from a CSV file or Excel, successive imports can be achieved with Import-CSV and Add-PnPList item cmdlets in PowerShell.
More info: Import CSV to SharePoint online list PowerShell
To upload a file to the SharePoint document library using PowerShell, use Files.Add method in CSOM or Add-PnPFile cmdlet from PnP PowerShell.
More info: Upload files to SharePoint Online using PowerShell
Use the “List.GetItems” method or Get-PnPListItem from PnP PowerShell to query and get list items from SharePoint Online.
More info: SharePoint Online Get list items PowerShell