Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

Crud Operation In List Items Using Pnp Powershell

SharePoint Patterns and Practices (PnP) contains a library of PowerShell commands (PnP PowerShell) that allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online and SharePoint On-Premises. Let’s see how to do the basic CRUD operation using PNP.

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.

$siteurl="https://<tenant-name>.sharepoint.com" 

Connect-PnPOnline -Url $siteurl

PNP Login

To Create a list Item:

The list item can be created by “Add-PnPListItem” cmdlet in a SharePoint site.

The required parameters are,

Leaderboard_CDR

List - The ID , Title, or Url of the list .

Values - Use the internal names of the fields when specifying field names.

1. Single line of text: -Values @{"Title" = "Title New"}

2. Multiple lines of text: -Values @{"MultiText" = "New text\n\More text"}

3. Rich text: -Values @{"MultiText" = "<strong>New</strong> text"}

4. Choice: -Values @{"Choice" = "Value 1"}

5. Number: -Values @{"Number" = "10"}

The Optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.

ContentType - Specify either the name, ID or an actual content type.

Folder - The list relative URL of a folder. E.g. "MyFolder" for a folder located in the root of the list, or "Folder/SubFolder" for a folder located in the MyFolder folder which is located in the root of the list.

Web - This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter is not required and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web.

The following code snippet helps to create SharePoint List item

Add-PnPListItem -List "HubflyTeam" -Values

@{"Title"="Hubfly";"NameHF"="Ravishankar";"AgeHF"="21";"RoleHF"="Developer"}

PnPListItem

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

The Option parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection

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

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

Web- This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter is not required and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web.

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 "---------------------------"

    }

WriteHost

Using CAML 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.

$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 "---------------------------"

}

FieldRef

To Update a SharePoint List Item:

The following code snippet helps to update SharePoint List item.The list Item can be updated by “Set-PnPListItem” on the SharePoint site. It Applies to SharePoint Server 2013, SharePoint Server 2016, SharePoint Online.

The required parameters are,

Identity - The ID of the list item , or actual list item object

List - The ID , Title, or Url of the list

Values - Use the internal names of the fields when specifying field names.

Single line of text: -Values @{"Title" = "Title New"}
Multiple lines of text: -Values @{"MultiText" = "New text\n\More text"}
Rich text: -Values @{"MultiText" = "<strong>New</strong> text"}
Choice: -Values @{"Choice" = "Value 1"}
Number: -Values @{"Number" = "10"}

The optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -Return Connection on Connect-PnPOnline or by executing Get-PnPConnection

ContentType - Specify either the name,ID, or an actual Content type

SystemUpdate - Update the item without creating a new version (Only applicable to SharePoint Online)

Web - This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter is not required and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web. 

The following code snippet helps to update SharePoint List item.

Set-PnPListItem -List HubflyTeam -Identity 3 -Values @{"NameHF" = "Arut"}

SharePoint List item

To Delete a List Item:

The list items can be deleted by “Remove-PnPListItem” cmdlet on the SharePoint site.  It applies to SharePoint Server 2013, SharePoint Server 2016, SharePoint Online.

The required parameters are,

Identity - The ID of the list item, or actual ListItem object

List - The ID , Title, or Url of the list

The optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -Return Connection on Connect-PnPOnline or by executing Get-PnPConnection.Force - Specifying the Force parameter will skip the confirmation question.

The following code snippet helps to update SharePoint List item.

Remove-PnPListItem -List HubflyTeam -Identity 3

Remove-PnPListItem

Thus, you have learned how to create, read,update, and delete list items programmatically  SharePoint site. PnP PowerShell is supported by SharePoint Online. The operations mentioned above, are tested on SharePoint Online environments.