Simple SharePoint Crud with PnP

Simple SharePoint Crud with PnP

Simple SharePoint
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!Confused Sharepoint
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:
  1. What is PnP PowerShell
  2. Installation of PnP PowerShell
  3. PnP commands Structure (similar to learning Abc)
  4. Writing PnP Commands (similar to writing words)
  5. 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 here
The 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 type


For 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

CRUDPNP PowerShell Verb
CreateAdd/New
ReadGet
UpdateSet
DeleteRemove
SPObject:
Below are some sample SharePoint Objects.
  1. Site
  2. List
  3. File
Suppose, you want to add a Website. The command is “New-PnPWeb”.

CRUD FOR List

You can search for PnP commands using


Even 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*
 CreateReadUpdateDelete
ListNew-PnPListGet-PnPListSet-PnPListRemove-PnPList
List ItemAdd-PnPListItemGet-PnPListItemSet-PnPListItemRemove-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:
  1. Create a Garden site
  2. Create a Fruits List with column Location
  3. Add few fruits
  4. Delete One Fruit


You can view code from here

Advantages

  1. Works on both Online and On Premise
  2. One line of code to do common tasks
  3. Simple install
  4. Has very powerful codes for complex coding in single line like Sending mail, adding scripts using Custom Action, enable Responsive Design.

Disadvantages

  1. 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?
  1. Action on Sites like creating sites using PnPProvisioningTemplate
  2. Upload Files and folders to Document Library
  3. Create lists and libraries from code
  4. Custom Action to show Subsite from Gear icon
Hope my words help you to become an Artist in PnP PowerShell coding!

Comments

Popular posts from this blog

SharePoint File CRUD in REST

Resting in Peace with Rest Api In SharePoint