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,
The following code snippet helps to connect SharePoint sites.
- $siteurl="https://<tenant-name>.sharepoint.com"
- Connect-PnPOnline -Url $siteurl
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.
- $listItems= (Get-PnPListItem -List HubflyTeam -Fields "Title","NameHF", "AgeHF","RoleHF","GUID")
- foreach($listItem in $listItems){
- Write-Host "Name" : $listItem["NameHF"]
- Write-Host "Age" : $listItem["AgeHF"]
- Write-Host "Role" : $listItem["RoleHF"]
- Write-Host "---------------------------"
- }
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.
- $listItems= Get-PnPListItem -List HubflyTeam -Query "<View><Query><Where><Eq><FieldRef Name='NameHF'/><Value Type='Text'>Ravishankar</Value></Eq></Where></Query></View>"
- foreach($listItem in $listItems){
- Write-Host "Name" : $listItem["NameHF"]
- Write-Host "Age" : $listItem["AgeHF"]
- Write-Host "Role" : $listItem["RoleHF"]
- Write-Host "---------------------------"
- }
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.