In Focus

PowerShell - SharePoint Online List Item CRUD Operation

I have written a few code samples that will help you to perform the CRUD operation on a SharePoint list using PnP PowerShell. This is helpful to the beginners who are starting to work on SharePoint.

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.
 
PowerShell - SharePoint Online List Item CRUD Operation 
Step 3
  1. //Add below Script to Connect to SharePoint Online.  
  2. #  
  3. Site Collection URL  
  4. $siteurl = "Your Site URL"#  
  5. User Credentials  
  6. $credential = Get - Credential# Connects and Creates Context  
  7. Connect - PnPOnline - Url $siteurl - Credentials $credential  
  8. //Add below Script to retrieve data from SharePoint list.  
  9. # Retrive List Item  
  10. $listItems = Get - PnPListItem - List "MyCustomList" - Fields "Title""Description"  
  11. foreach($listItem in $listItems) {  
  12.     Write - Host "Id : "  
  13.     $listItem["ID"]  
  14.     Write - Host "Title : "  
  15.     $listItem["Title"]  
  16.     Write - Host "Description : "  
  17.     $listItem["Description"]  
  18.     Write - Host "------------------------------------"  
  19. }  
  20. //Retrieve data from SharePoint list based on CAML Query  
  21. # Retrive List Item Using CAML Query  
  22. $listItems = Get - PnPListItem - List "MyCustomList" - Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Title 1</Value></Eq></Where></Query></View>"  
  23. foreach($listItem in $listItems) {  
  24.     Write - Host "Id : "  
  25.     $listItem["ID"]  
  26.     Write - Host "Title : "  
  27.     $listItem["Title"]  
  28.     Write - Host "Description : "  
  29.     $listItem["Description"]  
  30.     Write - Host "------------------------------------"  
  31. }  
  32. //Add data in SharePoint list  
  33. # Add List Item  
  34. Add - PnPListItem - List "MyCustomList" - Values @ {  
  35.     "Title" = "Added Title from Powershell";  
  36.     "Description" = "This is description which was added from Powershell"  
  37. }  
  38. //Update data in SharePoint list   
  39. # Update List Item Using Item ID  
  40. Set - PnPListItem - List "MyCustomList" - Identity 6 - Values @ {  
  41.     "Title" = "Updated Title from Powershell"  
  42. }  
  43. //Update data in SharePoint listUsing CAML Query  
  44. # Update List Item Using CAML Query  
  45. $item = Get - PnPListItem - List "MyCustomList" - Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Title 1</Value></Eq></Where></Query></View>"  
  46. if ($item - ne $null) {  
  47.     Set - PnPListItem - List "MyCustomList" - Identity $item - Values @ {  
  48.         "Title" = "Updated Title"  
  49.     }  
  50. }  
  51. //Delete data in SharePoint list  
  52. #Delete List Item  
  53. 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!!