Retrieve SharePoint List Items Using PnP PowerShell

In this blog we are going to see how to retrieve SharePoint list items using PnP PowerShell. PnP provisioning is a community-driven platform that enables fast development of components that define your application infrastructure & the content to some extent. We use CSOM and can work against both SharePoint Online and SharePoint On-Premises.

Prerequisite

Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

Connect to Site

Connect to SharePoint site using Connect-PnPOnline cmdlet. The required parameters are,

  • Url - The SharePoint site url (Eg: https://hubflysoft.sharepoint.com/sites/Hubfly)

The following code snippet helps to connect SharePoint sites.

  1. $siteurl="https://<tenant-name>.sharepoint.com"
  2. Connect-PnPOnline -Url $siteurl
SharePoint  

To Read a List Item

The list items can be read by “Get-PnPListItem” on the SharePoint Site. It applies to SharePoint Server 2013, SharePoint Server 2016, SharePoint Online.

The required parameters are,

  • List - The list to query
  • Fields - The fields to retrieve. If not specified all fields will be loaded in the returned list object

The Option parameters are,

  • Id- The ID of the item to retrieve
  • PageSize -The number of items to retrieve per page request.
  • Query- The CAML query to execute against the list
  • ScriptBlock - The script block to run after every page request
  • UniqueId- The unique id (GUID) of the item to retrieve

The following code snippet helps to read SharePoint List item.

  1. $listItems= (Get-PnPListItem -List HubflyTeam -Fields "Title","NameHF""AgeHF","RoleHF","GUID")  
  2. foreach($listItem in $listItems){  
  3.    Write-Host "Name" : $listItem["NameHF"]  
  4.    Write-Host "Age" : $listItem["AgeHF"]  
  5.    Write-Host "Role" : $listItem["RoleHF"]  
  6.    Write-Host "---------------------------"  
  7. }  
SharePoint  

Using CAML Query

CAML- Collaborative Application Markup Language. It is used to query SharePoint list items and filters specific data based on the requirements

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

  1. $listItems= Get-PnPListItem -List HubflyTeam -Query "<View><Query><Where><Eq><FieldRef Name='NameHF'/><Value Type='Text'>Ravishankar</Value></Eq></Where></Query></View>"  
  2. foreach($listItem in $listItems){  
  3.    Write-Host "Name" : $listItem["NameHF"]  
  4.    Write-Host "Age" : $listItem["AgeHF"]  
  5.    Write-Host "Role" : $listItem["RoleHF"]  
  6.    Write-Host "---------------------------"  
  7. }  
SharePoint  

Thus, you have learned how to retrieve SharePoint list items programmatically on SharePoint site. PnP PowerShell is supported by SharePoint Online. The operations mentioned above, are tested on SharePoint Online environments. Feel free to fill up the comment box below, if you need any assistance.