SharePoint Basic List Operations Using PnP PowerShell

Introduction

In this article, you will learn how we can create, retrieve, update and delete 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 for SharePoint 2013 on-premise or Office 365 versions.

Connect To Site

Connect to the site, using the snippet given below. The below PnP PowerShell code 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

The SharePoint list can be created on root site or sub sites by setting the context using site URL. PnP CSOM PowerShell can be used to complete the operation. 

New-SPOList command is used to create list on SharePoint sites. The necessary parameters to create the list are Title, URL and template. The predefined SharePoint list templates can be used to set the template type. In my example, I have used custom list template (generic list). The other parameters that can be passed are enabling quick launch option, and enabling content types.

The following command snippet helps creating a custom list with template id 100.
  1. function CreateList(){  
  2.     New-SPOList -Title "PnPList" -Template GenericList -Url "Lists/PnPList"  
  3. }  
  4. CreateList # Creates list   

Retrieve Lists

The lists available on root site collections or sub sites can be retrieved using PnP CSOM Powershell. We will see how we can retrieve the properties of SharePoint lists.

Get-SPOList is used to retrieve lists from sites. Along with the command, the list name is passed to retrieve the exact list information. When the command is just passed without any parameters, all the lists available on SharePoint site will be retrieved.

The basic properties which can be retrieved using the command are title, description, base template id, created date, list Id, image URL, and total list items count.

The following code snippet shows how we can retrieve all lists from the site.

  1. function RetrieveLists(){  
  2.     $lists = Get-SPOList  
  3.     #$list = $lists[0]  
  4.     foreach($list in $lists){  
  5.               
  6.         Write-Host "Title : " $list.Title  
  7.         Write-Host "Description : " $list.Description  
  8.         Write-Host "BaseTemplate ID : " $list.BaseTemplate  
  9.         Write-Host "Created Date : " $list.Created  
  10.         Write-Host "List ID : " $list.Id  
  11.         Write-Host "ImageUrl : " $list.ImageUrl  
  12.         Write-Host "ItemCount : " $list.ItemCount  
  13.         Write-Host "OnQuickLaunch : " $list.OnQuickLaunch  
  14.         Write-Host "------------------------------------"  
  15.     }  
  16.       
  17. }  
  18. RetrieveLists #Retrieves all lists from SP site   

The following code snippet shows how we can retrieve a particular list from the site using list name.

  1. function RetrieveList(){  
  2.     $list = Get-SPOList "PnPList"  
  3.     Write-Host "Title : " $list.Title  
  4.     Write-Host "Description : " $list.Description  
  5.     Write-Host "BaseTemplate ID : " $list.BaseTemplate  
  6.     Write-Host "Created Date : " $list.Created  
  7.     Write-Host "List ID : " $list.Id  
  8.     Write-Host "ImageUrl : " $list.ImageUrl  
  9.     Write-Host "ItemCount : " $list.ItemCount  
  10.     Write-Host "OnQuickLaunch : " $list.OnQuickLaunch  
  11.       
  12. }  
  13. RetrieveList #Retrieves list using list name   

Update List

The properties of list available on root site collections or sub sites can be updated using PnP CSOM Powershell. We will see how we can update the properties.

Set-SPOList is used to update the properties of list. The necessary parameter used to update the property is Identity. The list name should be passed in the identity. In my example, Title and permissions of list are updated.

The following code snippet shows how we can update the list properties on the site.

  1. function UpdateList(){  
  2.     # Update the identity value to your corresponding list name (title)  
  3.     Set-SPOList -Identity "PnPList" -Title "PnPList1" -BreakRoleInheritance  
  4.       
  5. }  
  6. UpdateList # Updates list properties   

Delete List

The lists available on root site collections or sub sites can be deleted using PnP CSOM Powershell. We will see how we can delete a list.

Remove-SPOList command is used to delete a list. Identity should be passed as parameter for deleting list. The list name should be passed in the identity. You will be prompted for delete confirmation. Force parameter can be used to force delete the list.

The following code snippet shows how we can delete a list on the site.

  1. function DeleteList(){  
  2.     # Update the identity value to your corresponding list name (title)  
  3.     #Remove-SPOList -Identity "PnPList1"  
  4.     Remove-SPOList -Identity "PnPList" -Force  
  5. }  
  6. DeleteList # Deletes List with list name   

Summary

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