Create, read, update, and delete (CRUD) is an acronym used in database-related functions. James Martin probably coined the term (CRUD) in his 1983 book Managing the Database Environment.
We can use the following models to execute basic CRUD operations in SharePoint:
- REST-API Object Model.
- Client-Side Object Model
- Server-Side Object Model
We must write the code in Visual Studio, VS Code, or write a script in the script editor web part to perform CRUD activities by utilizing the previously described approach. There are times when we must conduct repetitive operations and cannot use the same piece of code each time. PowerShell comes in handy in this situation. The operation might be accomplished in less time and with fewer lines of code using PowerShell.
PowerShell is an all-in-one command-line shell and scripting language. It was created as a task engine that wraps tasks that individuals must do with cmdlets. You can use PowerShell to run commands on both local and remote machines. This sophisticated application largely replaced the Command Prompt as the default way to automate batch processes and develop customized system management tools when it was released in 2006. You can do administrative tasks, manage users, and automate workflows. The list goes on.
We only need the default Windows PowerShell tool and admin access to the SharePoint site where we need to do the operation, or we can use the link below to get SharePoint Online Management Shell.
You can download SharePoint Online Management Shell here.
Follow these steps
Open Window PowerShell ISE in admin mode and install PnP PowerShell command using this script: Paste the command in the window console and press enter:
Install-Module -Name “PnP.PowerShell”
Then, if you want to install or update to the latest nightly built prerelease of PnP PowerShell, run this code:
Install-Module -Name “PnP.PowerShell” -AllowPrerelease
To uninstall the PnP PowerShell, you can run the following code:
Uninstall-Module -Name “PnP.PowerShell”
Understanding what PowerShell is
Microsoft’s PowerShell is an automated robust scripting language based on the .NET framework.
Why you should use PnP PowerShell
SharePoint Online and on-premises are managed with PowerShell. Other forms of PowerShell scripts, such as SPFx, exist in addition to PnP PowerShell, which uses object model type scripting. The code length increases since we need to import a few SharePoint snap-in files to make it work.
When compared to SPFx PowerShell, PnP PowerShell requires fewer script lines. Microsoft and the community collaborate on PnP, which stands for “Patterns and Practices.”
Let’s look at how to perform CRUD operations in SharePoint Online using PnP PowerShell scripting.
You can execute commands without utilizing Google or Bing even if you have no knowledge of how to do it. To find documentation for a command, run the following script:
Get-Help Get-PnPFile -examples
PnP PowerShell Script for CRUD Operation
The PnP script completes the following:
- Connects to a SharePoint site using PnP PowerShell
- Retrieves list items from SharePoint List
- Adds item in SharePoint List
- Updates item in SharePoint List
- Deletes item from the list column
The above script can be customized to meet your specific needs. This script will add, remove, and update entries from a SharePoint list.
Looking at different SharePoint Outputs
Adding an item to the SharePoint list:
Updating an item in a SharePoint list:
Removing an Item from a SharePoint List:
Below are some of the benefits of working with PnP:
- It works online and on-premises
- It only needs one line of code to do typical functions on both online and on-premises
- It has a comprehensive collection of instructions that enable sophisticated coding in a single line, such as sending mail, inserting scripts using “custom action”, and enabling “responsive design.”
We can apply PnP to the following areas below:
- Adding documents and folders to the document library
- Creating document libraries/lists
- Building sites, subsites, content types, site columns, and more
My Final Thoughts
Although PowerShell is a sophisticated scripting language, getting a firm hold on it might take some time, especially if you are unfamiliar with the .NET Framework. I hope that anyone learning PowerShell will find this essay and script helpful. Have fun coding!
For any other questions, contact our commerce experts today.