Simple SharePoint Crud with PnP
Simple SharePoint Crud with PnP

Simple is Superb. I have driven in Pittsburgh and missed the simple straight highways. There are various ways to do CRUD coding in SharePoint. I am using PowerShell to explain the CRUD development using CSOM,REST etc. Here, I am focussing on the simplest of all CRUD operations for developers with one line of code.
PnP PowerShell
Working on PnP PowerShell is applicable even to administrators with little coding knowledge who can focus on writing their ideas in single lines, similar to writing sentences in English. If you want to create a site without clicking often from UI, you can use the following command:
Many times, I forget where is that code/document I created. I did some work in some folder and forgot the location and remember just the name!

Example 2, if you want to get a report of all items in SharePoint with Title “garden” here is one line code.
Instead of writing long lines of code or use complex softwares like Visual Studio,SharePoint Designer, you can use simple PowerShell which comes for free in Windows. You save time and money spent on many tools on analysis, design,installing, coding, testing. When you finish, another way of development comes to fix the issues in complex tool or the support for the tools ends from Microsoft. You are a One Man/Woman Army for your project/ideas with few lines of code with no worries about the tool dependence.
After reading this article and listening to the video, you will learn the following:
- What is PnP PowerShell
- Installation of PnP PowerShell
- PnP commands Structure (similar to learning Abc)
- Writing PnP Commands (similar to writing words)
- Mixing PnP commands (similar to creating sentences)
What is PnP PowerShell
Many from Microsoft and community like Eric have created stunning codes to get your job done faster in one line. You can have look at the codes from hereThe full form for PNP is Patterns and Practices from Microsoft. However,I would call PnP as “Put an eNd to Problems” from long coding.
Though I personally love CSOM coding, when I want to do something very fast, I had to use PnP to create new sites from existing site templates using Provisioning Template commands. The commands to do actions are meaningful and self explanatory.
Install
This article focuses on SharePoint Online. To install, open PowerShell ISE as Administrator and typeFor On Premise
Updates
Once the install is done, get connected to your SharePoint site
Structure of PnP Command
[verb]-PnP[SpObject]
A PnP consists of the following:- Verb
- Get
- Set
- Remove
- New
- Add
- PnP
- SPObject
- Web
- List
- ListItem
- File
- …Many more
Common Verbs
CRUD | PNP PowerShell Verb |
Create | Add/New |
Read | Get |
Update | Set |
Delete | Remove |
Below are some sample SharePoint Objects.
- Site
- List
- File
CRUD FOR List
You can search for PnP commands usingEven if you do not know how to execute the chosen command, you can execute the commands without using Google search. How to get help on executing the command you have chosen for your need?
You execute one of the examples based on your needs and see the output.
Below are CRUD commands for List. Try guessing the CRUD command for items like File and check the result from Get-Command *PnPFile*
Create | Read | Update | Delete | |
List | New-PnPList | Get-PnPList | Set-PnPList | Remove-PnPList |
List Item | Add-PnPListItem | Get-PnPListItem | Set-PnPListItem | Remove-PnPListItem |
Creating a Garden Site and Fruits List
Lets apply what you learnt from above. After learning words, you join the words in different permutations and combinations and get sentences. Similarly, you create meaningful applications by using PnP commands.Lets go ahead do the following:
- Create a Garden site
- Create a Fruits List with column Location
- Add few fruits
- Delete One Fruit
You can view code from here
Advantages
- Works on both Online and On Premise
- One line of code to do common tasks
- Simple install
- Has very powerful codes for complex coding in single line like Sending mail, adding scripts using Custom Action, enable Responsive Design.
Disadvantages
- Some commands may not work sometimes. Example, Add-PnPListItem. Alternate solution is Set-PnPListItem and Add-PnPListItem
Real World Applications:
What are some of the application using PnP in real world for me?- Action on Sites like creating sites using PnPProvisioningTemplate
- Upload Files and folders to Document Library
- Create lists and libraries from code
- Custom Action to show Subsite from Gear icon
Comments
Post a Comment