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
To add a list item using PowerShell, we've to set value for each custom column in the list. Here is the SharePoint Online PowerShell to create a list item
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
PnP PowerShell to Add List Item in SharePoint Online
Here is how to add item to list in SharePoint Online with PnP PowerShell
How about Setting Fields like Managed metadata, Hyperlink, Lookup, Person or Group, etc?
The above scripts works just fine with simple field types such as Single lines of text, choice, Yes/No, etc. However, for other field types such as Managed metadata, Hyperlink, Lookup, People Picker, etc. we've to handle them differently. Here are my posts to help:
SharePoint Online: PowerShell to Add List Item
To add a list item using PowerShell, we've to set 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://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("[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% #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 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"}More on Add-PnPListItem
How about Setting Fields like Managed metadata, Hyperlink, Lookup, Person or Group, etc?
The above scripts works just fine with simple field types such as Single lines of text, choice, Yes/No, etc. However, for other field types such as Managed metadata, Hyperlink, Lookup, People Picker, etc. we've to handle them differently. 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
No comments:
Please Login and comment to get your questions answered!