Introduction
In this article, you will learn how we can retrieve, create, delete or update list items using CSOM with PowerShell. This is mainly focused on using PowerShell scripts for any SharePoint sites.
Note: I have explained all the logic with respect to SharePoint online sites. If you want to do the same operation for SharePoint on premise sites, you need to remove credentials parameter from the context and run it on the respective on premise servers.
Get List Items
First we will see how we can get the existing list items available for a list on the SharePoint site. The steps followed here are very similar to the steps following CSOM or JSOM programming.
- Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
- Then access the list using the context, initialize the query and use GetItems method to get items.
- Load the objects and execute the query.
- Loop through the result object and get the necessary field values.
- 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"
-
- $siteURL = ""
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try{
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("TestList")
- $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
- $ctx.load($listItems)
-
- $ctx.executeQuery()
- foreach($listItem in $listItems)
- {
- Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"]
- }
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
This will get all the list items of the list on the site.
Next, we will see how we can create list items.
Create List Item
Here we will see how we can create item in a list. The following steps depict the flow.
- Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
- Initialize the ListItemCreationInformation object.
- Set the required parameters for new list item.
- Add new list item creation object to the list item collection. Then Load and execute the query.
- 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"
-
- $siteURL = ""
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try{
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("TestList")
- $listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
- $listItem = $list.AddItem($listItemInfo)
- $listItem["Title"] = "c"
- $listItem.Update()
- $ctx.load($list)
- $ctx.executeQuery()
- Write-Host "Item Added with ID - " $listItem.Id
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
The list item will be added. Next, we will see how to delete list.
Delete List Item
Here we will see how we can delete the list item. The following steps depict to you the flow.
- Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
- Get the list item using GetItemById method.
- Then, remove the list item using the delete object method.
- Using the context, execute the query.
- 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"
-
- $siteURL = ""
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try{
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("TestList")
- $listItem = $list.GetItemById(1)
- $listItem.DeleteObject()
- $ctx.executeQuery()
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
The list item will be deleted from the list.
Update List Item
Here we will see how we can update the list item. The following steps depict to you the flow.
- Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
- Get the list using GetItemById method.
- You can do your own custom operation with your custom logic here.
- Then, update the list item using the update method.
- Using the context, execute the query.
- 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"
-
- $siteURL = ""
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- $ctx.credentials = $creds
- try{
- $lists = $ctx.web.Lists
- $list = $lists.GetByTitle("TestList")
- $listItem = $list.GetItemById(1)
- $listItem["Title"] = "aa"
- $listItem.Update()
- $ctx.load($listItem)
- $ctx.executeQuery()
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
The list item will be updated. Likewise you can do your own update operations.
Summary
Thus you have learned how to create, read, update or delete list items on SharePoint Online or O365 or on premise sites using CSOM with PowerShell commands.