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).
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$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.
function CreateList(){
New-SPOList -Title "PnPList" -Template GenericList -Url "Lists/PnPList" }
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.
function RetrieveLists(){
$lists = Get-SPOList
#$list = $lists[0]
foreach($list in $lists){
Write-Host "Title : " $list.Title
Write-Host "Description : " $list.Description
Write-Host "BaseTemplate ID : " $list.BaseTemplate
Write-Host "Created Date : " $list.Created
Write-Host "List ID : " $list.Id
Write-Host "ImageUrl : " $list.ImageUrl
Write-Host "ItemCount : " $list.ItemCount
Write-Host "OnQuickLaunch : " $list.OnQuickLaunch
Write-Host "------------------------------------" } }
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.
function RetrieveList(){
$list = Get-SPOList "PnPList"
Write-Host "Title : " $list.Title
Write-Host "Description : " $list.Description
Write-Host "BaseTemplate ID : " $list.BaseTemplate
Write-Host "Created Date : " $list.Created
Write-Host "List ID : " $list .Id
Write-Host "ImageUrl : " $list.ImageUrl
Write-Host "ItemCount : " $list.ItemCount
Write-Host "OnQuickLaunch : " $list.OnQuickLaunch }
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.
function UpdateList(){
# Update the identity value to your corresponding list name (title)
Set-SPOList -Identity "PnPList" -Title "PnPList1" -BreakRoleInheritance }
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.
function DeleteList(){
# Update the identity value to your corresponding list name (title)
# Remove-SPOList -Identity "PnPList1"
Remove-SPOList -Identity "PnPList" -Force }
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.
Comments