Introduction
Many times, we need to perform some basic CRUD operation in a SharePoint Online list item.
I have written a few code samples to help you perform the CRUD operations on SharePoint List using PnP PowerShell.
Step 1
Download SharePoint Online Management Shell from
here.
Step 2
Create a list in SP Online or you can use an existing list for operations with a description added as a column in the list. And add some records to the list.
Step 3
-
- #
- Site Collection URL
- $siteurl = "Your Site URL"#
- User Credentials
- $credential = Get - Credential# Connects and Creates Context
- Connect - PnPOnline - Url $siteurl - Credentials $credential
-
- # Retrive List Item
- $listItems = Get - PnPListItem - List "MyCustomList" - Fields "Title", "Description"
- foreach($listItem in $listItems) {
- Write - Host "Id : "
- $listItem["ID"]
- Write - Host "Title : "
- $listItem["Title"]
- Write - Host "Description : "
- $listItem["Description"]
- Write - Host "------------------------------------"
- }
-
- # Retrive List Item Using CAML Query
- $listItems = Get - PnPListItem - List "MyCustomList" - Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Title 1</Value></Eq></Where></Query></View>"
- foreach($listItem in $listItems) {
- Write - Host "Id : "
- $listItem["ID"]
- Write - Host "Title : "
- $listItem["Title"]
- Write - Host "Description : "
- $listItem["Description"]
- Write - Host "------------------------------------"
- }
-
- # Add List Item
- Add - PnPListItem - List "MyCustomList" - Values @ {
- "Title" = "Added Title from Powershell";
- "Description" = "This is description which was added from Powershell"
- }
-
- # Update List Item Using Item ID
- Set - PnPListItem - List "MyCustomList" - Identity 6 - Values @ {
- "Title" = "Updated Title from Powershell"
- }
-
- # Update List Item Using CAML Query
- $item = Get - PnPListItem - List "MyCustomList" - Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Title 1</Value></Eq></Where></Query></View>"
- if ($item - ne $null) {
- Set - PnPListItem - List "MyCustomList" - Identity $item - Values @ {
- "Title" = "Updated Title"
- }
- }
-
- #Delete List Item
- Remove - PnPListItem - List "MyCustomList" - Identity 6 - Force
You can use the above PowerShell script as per your requirement. You can also refer to the file attached with this blog for your reference.
Cheers!!