SharePoint Basic List Item Operations Using PnP PowerShell

Introduction

In this article, you will learn how we can create, retrieve, update and delete the items on SharePoint lists using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite

You need to have PowerShell 3.0 available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentations on the official site. The installers are available here.

The following operations will be compatible with SharePoint 2013 on-premise or Office 365 versions.

Connect To Site

Connect to the site, using the snippet given below. The PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM). 

  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  

Once connected, you can carry out any of the operations mentioned below, based on the requirement. 

Create List Item

The list items can be added to the root site or sub site lists by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.

Add-SPOListItem command is used to create the items on SharePoint list. The new values can be passed as hash tables through the values parameter. In my example, I have added a new item with two custom column values. When the hash table is not passed as a parameter, the new item will be created with the empty field values.

The following command snippet helps in adding the new items to a custom list.

  1. function AddListItem(){  
  2.     # Creates item with empty fields  
  3.     Add-SPOListItem -List "PnPList"   
  4.     # Creates item with values  
  5.     Add-SPOListItem -List "PnPList" -Values @{"Title"="6";"ProductId"="6"}  
  6. }  
  7. AddListItem # Adds List item   

Retrieve List Items

The list items, available on the root site collections or sub site lists, can be retrieved by setting the context, using PnP CSOM PowerShell. We will see how we can retrieve the items from SharePoint lists.

Get-SPOListItem is used to retrieve the items from the lists. Along with the command, the list name and the required fields are passed. The column names are passed, using a field parameter. The list item collection will be retrieved. Using the  for each loop, we can get all the item details. When the field names are not passed, all the field values for items are retrieved.

The following code snippet shows how we can retrieve all the items from SharePoint list:

  1. function RetrieveListItems(){  
  2.     $listItems = Get-SPOListItem -List "PnPList" -Fields "Title","ProductId"  
  3.     foreach($listItem in $listItems){  
  4.               
  5.         Write-Host "Title : " $listItem["Title"]  
  6.         Write-Host "Id : " $listItem["ID"]  
  7.         Write-Host "ProductId : " $listItem["ProductId"]  
  8.         Write-Host "------------------------------------"  
  9.     }  
  10. }  
  11. RetrieveListItems # Get Items  
Using Query

Queries can be used to filter SharePoint list items, based on the requirements. The query can be sent with Query parameters for filtering data.

The following code snippet shows how we can retrieve all items from the SharePoint list using queries.

  1. function RetrieveListItemByQuery(){  
  2.     $listItem = Get-SPOListItem -List PnPList -Query "<View><Query><Where><Eq><FieldRef Name='ProductId'/><Value Type='Text'>2</Value></Eq></Where></Query></View>"  
  3.     Write-Host "Title : " $listItem["Title"]  
  4.     Write-Host "Id : " $listItem["ID"]  
  5.     Write-Host "ProductId : " $listItem["ProductId"]  
  6.     Write-Host "------------------------------------"  
  7. }  
  8. RetrieveListItemByQuery # Retrieve items by query   
Update List Item

SharePoint list items available on the root site collections or sub sites can be updated, using PnP CSOM Powershell. 

Set-SPOListItem is used to update the items, using PnP PowerShell. The necessary parameter is used to update the item regarded as the list name and identity (list item id or item variable). The values are passed as hash tables through values parameter.

The following code snippet shows how we can update the items on the list, using PnP PowerShell:

  1. function UpdateListItem(){  
  2.     Set-SPOListItem -List "PnPList" -Identity 6 -Values @{"Title"="6A";"ProductId"="product6"}  
  3. }  
  4. UpdateListItem # Update item by identity  

Using query

The items can be updated, using query, as well. Retrieve the items, using query (refer to the above section). The retrieved item is then passed as an identity with the update command.

The following code snippet shows how we can update the items on the list with a query, using PnP PowerShell:

  1. function UpdateListItemByQuery(){  
  2.     # Update using query  
  3.     $item = Get-SPOListItem -List "PnPList" -Query "<View><Query><Where><Eq><FieldRef Name='ProductId'/><Value Type='Text'>product6</Value></Eq></Where></Query></View>"  
  4.     if($item -ne $null){  
  5.         Set-SPOListItem -List "PnPList" -Identity $item -Values @{"Title"="6";"ProductId"="6"}  
  6.     }  
  7. }  
  8. UpdateListItemByQuery # Update item by query   

Delete List Item

The list items can be deleted using PnP CSOM PowerShell.

Remove-SPOListItem command is used to delete a list item. The required parameters for deleting the list items are list name and identity value. Once the command is executed, the alert box will be prompted for the confirmation. The force attribute is used to delete the list items; i.e., without a confirmation prompt.

The following code snippet shows how we can delete the items in a SharePoint list.

  1. function DeleteListItem(){  
  2.     Remove-SPOListItem -List "PnPList" -Identity 5      
  3.     Remove-SPOListItem -List "PnPList" -Identity 4 -Force # Delete by Using force attribute  
  4. }  
  5. DeleteListItem # Delete item by identity  

Using Query

The items can be deleted using query as well. Retrieve the items, using query (refer above section). The retrieved item is passed as an identity with the delete command.

The following code snippet shows how we can delete the items on the list with the query, using PnP PowerShell.

  1. function DeleteListItemByQuery(){  
  2.     # Delete using query  
  3.     $item = Get-SPOListItem -List "PnPList" -Query "<View><Query><Where><Eq><FieldRef Name='ProductId'/><Value Type='Text'>6</Value></Eq></Where></Query></View>"  
  4.     if($item -ne $null){  
  5.         Remove-SPOListItem -List "PnPList" -Identity $item  
  6.     }  
  7. }  
  8. DeleteListItemByQuery # Delete item by query   

Summary

Thus, you have learned how to create, retrieve, update or delete the SharePoint list items, using PnP PowerShell on SharePoint 2013 / SharePoint online sites.