List Item Operations Using CSOM with PowerShell For SharePoint Online

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.

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
}
  1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
  2. Then access the list using the context, initialize the query and use GetItems method to get items.
  3. Load the objects and execute the query.
  4. Loop through the result object and get the necessary field values.

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.

  1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
  2. Initialize the ListItemCreationInformation object.
  3. Set the required parameters for new list item.
  4. 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.

  1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
  2. Get the list item using GetItemById method.
  3. Then, remove the list item using the delete object method.
  4. 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")
        $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 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.

  1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
  2. Get the list using GetItemById method.
  3. You can do your own custom operation with your custom logic here.
  4. Then, update the list item using the update method.
  5. 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.